Masonry父容器根据自容器宽度自适应报错

2018-12-25  本文已影响15人  独孤流

说明

在做一个UI界面中,开发了一个常见的布局,就是父容器根据自容器的宽度自适应,具体说明是:2的部分根据3的文字宽度来自适应,1根据2的宽度再自适应,如题如下:


111.png

但是添加好约束后报约束错误:

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.
(
<MASLayoutConstraint:0x60c0004ac420 UILabel:0x7fb175ab9dd0.left == UIView:0x7fb175ac4a70.left + 5>,
<MASLayoutConstraint:0x60c0004ab040 UILabel:0x7fb175ab9dd0.right == UIView:0x7fb175ac4a70.right - 15>,
<MASLayoutConstraint:0x60c0004ac240 UIView:0x7fb175ac4a70.left == xxxxTitleView:0x7fb175ac3c20.left + 20>,
<MASLayoutConstraint:0x60c0004ac4e0 UIView:0x7fb175ac4a70.right == xxxxTitleView:0x7fb175ac3c20.right>,
<NSLayoutConstraint:0x60000068e470 xxxTitleView:0x7fb175ac3c20.width == 0>,
)

具体原因是:是最外层1的容器本身没有约束,导致的问题,
解放方案:将1的那个view设置个属性translatesAutoresizingMaskIntoConstraints

// 将1的那个view,在这个页面中是navigationItem.titleView,进行下面的设置
xxx.translatesAutoresizingMaskIntoConstraints = NO;

参考:translatesAutoresizingMaskIntoConstraints 详解

上一篇下一篇

猜你喜欢

热点阅读