iOS自动布局视图以父级为中心,具有边距和动态高度

时间:2016-10-19 11:44:52

标签: ios dynamic autolayout margin center

我正在使用自动布局创建弹出窗口。我的弹出窗口具有动态高度,并且必须在窗口中居中,并带有边距。它有一个固定的宽度。

我添加了以下约束:

  • width =固定值
  • 高度小于或等于计算值
  • 上边距=集装箱上边距+固定价值
  • 底部边距=容器底部边距+固定值
  • center x = container center x
  • center y = container center y

它似乎有效,但Xcode显示警告:

  

无法同时满足约束条件。

     

以下列表中的至少一个约束可能是您不想要的约束。试试这个:(1)看看每个约束,并试着找出你不期望的东西; (2)找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您正在查看您不了解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性的文档translatesAutoresizingMaskIntoConstraints)

     

"<NSLayoutConstraint:0x7e572a40 V:|-(>=19)-[UIView:0x7a8bb6a0]   (Names: '|':UIView:0x7e572140 )>",
"<NSLayoutConstraint:0x7e573620 UIView:0x7a8bb6a0.bottom >= UIView:0x7e572140.bottom + 19>",
"<NSLayoutConstraint:0x7e573950 UIView:0x7a8bb6a0.centerY == UIView:0x7e572140.centerY>"
     

     

将尝试通过违反约束来恢复

<NSLayoutConstraint:0x7e573620 UIView:0x7a8bb6a0.bottom >= UIView:0x7e572140.bottom + 19>

出了什么问题?如果我删除Xcode中断的约束,它就不再起作用了(我的弹出窗口不再可见)。

这是我的约束代码:

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.width,
                                           relatedBy:NSLayoutRelation.equal,
                                           toItem:nil,
                                           attribute:NSLayoutAttribute.notAnAttribute,
                                           multiplier: 1,
                                           constant: UIPopup.defaultSize.width));

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.height,
                                           relatedBy:NSLayoutRelation.lessThanOrEqual,
                                           toItem:nil,
                                           attribute:NSLayoutAttribute.notAnAttribute,
                                           multiplier: 1,
                                           constant:popupHeight));

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.top,
                                           relatedBy:NSLayoutRelation.greaterThanOrEqual,
                                           toItem:self.view,
                                           attribute:NSLayoutAttribute.top,
                                           multiplier: 1,
                                           constant:verticalMargin));


self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.bottom,
                                           relatedBy:NSLayoutRelation.greaterThanOrEqual,
                                           toItem:self.view,
                                           attribute:NSLayoutAttribute.bottom,
                                           multiplier: 1,
                                           constant:verticalMargin));

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.centerX,
                                           relatedBy:NSLayoutRelation.equal,
                                           toItem:self.view,
                                           attribute:NSLayoutAttribute.centerX,
                                           multiplier: 1,
                                           constant: 0));

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.centerY,
                                           relatedBy:NSLayoutRelation.equal,
                                           toItem:self.view,
                                           attribute:NSLayoutAttribute.centerY,
                                           multiplier: 1,
                                           constant: 0));

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案:

  • width =固定值
  • 身高&lt; =父母身高 - 2 *保证金[优先级为1000]
  • height = popup&#34; real&#34;身高[具有800优先权]
  • center x = container center x
  • center y = container center y

代码:

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.width,
                                           relatedBy:NSLayoutRelation.equal,
                                           toItem:nil,
                                           attribute:NSLayoutAttribute.notAnAttribute,
                                           multiplier: 1,
                                           constant: UIPopup.defaultSize.width));

let maxHeightConstraint = NSLayoutConstraint(item:_mainContainer,
                                             attribute:NSLayoutAttribute.height,
                                             relatedBy:NSLayoutRelation.lessThanOrEqual,
                                             toItem:self.view,
                                             attribute:NSLayoutAttribute.height,
                                             multiplier: 1,
                                             constant:-2*verticalMargin);
maxHeightConstraint.priority = 1000;
self.view.addConstraint(maxHeightConstraint);

let heightConstraint = NSLayoutConstraint(item:_mainContainer,
                                          attribute:NSLayoutAttribute.height,
                                          relatedBy:NSLayoutRelation.equal,
                                          toItem:nil,
                                          attribute:NSLayoutAttribute.notAnAttribute,
                                          multiplier: 1,
                                          constant:popupHeight);
heightConstraint.priority = 800;
self.view.addConstraint(heightConstraint);

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.centerX,
                                           relatedBy:NSLayoutRelation.equal,
                                           toItem:self.view,
                                           attribute:NSLayoutAttribute.centerX,
                                           multiplier: 1,
                                           constant: 0));

self.view.addConstraint(NSLayoutConstraint(item:_mainContainer,
                                           attribute:NSLayoutAttribute.centerY,
                                           relatedBy:NSLayoutRelation.equal,
                                           toItem:self.view,
                                           attribute:NSLayoutAttribute.centerY,
                                           multiplier: 1,
                                           constant: 0));