iOS开发------手动约束布局出现NSAutoresizingMaskLayoutConstraint冲突(解决)

本文解决了一个iOS开发中遇到的问题:当使用Auto Layout时,不同屏幕尺寸下出现约束冲突的警告。通过调整约束的优先级,可以有效避免警告的出现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

十分感谢一下博客让我解决了问题:
ios 约束冲突NSAutoresizingMaskLayoutConstraint

最近手写约束布局代码,适配不同屏幕的时候,出现了一个很奇怪的问题,不报错也不崩溃,最终的效果也是理想的,但就是出警告。原因是约束的大小不固定,需要根据屏幕的比例大小进行相应的变值。

程序是在iPhone5s上进行的开发,在那个时候是没有问题的,但运行在iPhone6s的时候,就会出现下面的警告,但运行的结果是理想中的效果:

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-05-05 14:10:20.354 IGO[5581:2754283] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x13a093200 V:|-(10)-[UIImageView:0x13a0922d0]   (Names: '|':YBChildCell:0x13a08a8b0'YBChildObject' )>",
    "<NSLayoutConstraint:0x13a093280 V:[UIImageView:0x13a0922d0(82.0312)]>",
    "<NSLayoutConstraint:0x13a0932d0 V:[UIImageView:0x13a0922d0]-(10)-|   (Names: '|':YBChildCell:0x13a08a8b0'YBChildObject' )>",
    "<NSLayoutConstraint:0x13a09a2f0 'UIView-Encapsulated-Layout-Height' V:[YBChildCell:0x13a08a8b0'YBChildObject'(102)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x13a0932d0 V:[UIImageView:0x13a0922d0]-(10)-|   (Names: '|':YBChildCell:0x13a08a8b0'YBChildObject' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.


大体的意思就是约束出现冲突,看到这个警告的第一个反应就是组件的translatesAutoresizingMaskIntoConstraints属性没有设置为false,但是检查过后,没有出现这个错误。

虽然不影响最终的布局,但每次一运行就爆出那么多的警告,着实觉得非常的不好,于是百度了一下,从而解决方法如下:

找到提示冲突的语句

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x13a0932d0 V:
[UIImageView:0x13a0922d0]-(10)-|   (Names:
 '|':YBChildCell:0x13a08a8b0'YBChildObject' )>


调整此约束的优先级,如下:

NSArray * ver1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_headImageView(imageHeight)]-10-|"
                                                         options:0 metrics:metriceDictionary views:NSDictionaryOfVariableBindings(_headImageView)];

[ver1.firstObject setPriority:UILayoutPriorityDefaultHigh];//修改优先级


这样就不会出现那个警告了,具体原因,楼主表示真的不清楚,因为布局来看,是没有任何冲突的,如果有知道的人,也请告知一下…Thanks()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值