使用“自动布局”隐藏UIView时以编程方式添加约束



我正在尝试向XIB中名为contentContainerUIView添加一些约束。我是绿色的自动布局,但新的iPhone迫使我加入。很抱歉,但我没有10的声誉,所以堆栈不会让我发布XIB的图片,所以解释起来会更复杂。。。

contentContainer内是一个称为fixedContentContainerUIView。就容器内所有UIViews的高度而言,该容器每次都是相同的。我在IB中为该容器的内容添加了约束。那里一切都很好。fixedContentContainer下面的contentContainer中还有4个UIView对象:

  1. 一个称为friendsLabel的静态UILabel
  2. 一个名为tableViewUITableView
  3. 一个称为eventDescriptionHeaderLabel的静态UILabel
  4. 一个称为eventDescriptionLabel的动态UILabel

我想完成的很简单。如果用户打开了一个活动,但他们没有朋友,我会隐藏friendsLabeltableView,然后向上移动eventDescriptionHeaderLabeleventDescriptionLabel来填充空白。不过现在,我只想简单地开始。我只是试图以编程方式向friendsLabel添加约束,我可以在IB中添加这些约束,所以我显然做错了什么。(一旦一切正常,我很乐意上传其余的代码)。

以下是我尝试以编程方式添加约束的地方:

NSDictionary *viewsDictionary = @{@"fixedContentContainer":self.fixedContentContainer,
                                  @"friendsLabel":self.friendsLabel,
                                  @"tableView":self.tableView,
                                  @"eventDescriptionHeaderLabel":self.eventDescriptionHeaderLabel,
                                  @"eventDescriptionLabel":self.eventDescriptionLabel};
// Add 8px of space between bottom of fixedContentContainer and top of friendsLabel
[self.contentContainer addConstraints:
        [NSLayoutConstraint constraintsWithVisualFormat:@"V:[fixedContentContainer]-8-[friendsLabel]"
                                                options:0
                                                metrics:nil
                                                  views:viewsDictionary]];
// Add 15px to the left and 10px to the right of padding between friendsLabel and superview (contentContainer)
[self.contentContainer addConstraints:
        [NSLayoutConstraint constraintsWithVisualFormat:@"|-15-[friendsLabel]-10-|"
                                                options:0
                                                metrics:nil
                                                  views:viewsDictionary]];
// Set height for friendsLabel at 21px
[self.contentContainer addConstraints:
        [NSLayoutConstraint constraintsWithVisualFormat:@"V:[friendsLabel(21)]"
                                                options:0
                                                metrics:nil
                                                  views:viewsDictionary]];

顺便说一句,您是直接向UILabel添加高度约束,还是仍然向containerView添加高度约束?以下是我从控制台得到的(如果我通过IB添加一些确切的约束,我什么都得不到):

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

"<NSLayoutConstraint:0x84cafae0 EBWebImageView:0x84cafa40.width == 1.78448*EBWebImageView:0x84cafa40.height>",
"<NSLayoutConstraint:0x84cb04a0 V:[UILabel:0x84cb03a0'Kimberly Otte | Jenna Lau...'(26)]>",
"<NSLayoutConstraint:0x84cb0c50 V:[UIImageView:0x84cb0ba0(20)]>",
"<NSLayoutConstraint:0x84cb0f40 V:[UIImageView:0x84cb0e20(20)]>",
"<NSLayoutConstraint:0x84cb16b0 V:[UILabel:0x84cb1540'Event Details'(21)]>",
"<NSLayoutConstraint:0x84cb19a0 V:[UITableView:0x7c361e00(225)]>",
"<NSLayoutConstraint:0x84cb2fc0 H:[EBWebImageView:0x84cafa40]-(0)-|   (Names: '|':UIView:0x84caf9a0 )>",
"<NSLayoutConstraint:0x84cb2ff0 H:|-(0)-[EBWebImageView:0x84cafa40]   (Names: '|':UIView:0x84caf9a0 )>",
"<NSLayoutConstraint:0x84cb3020 V:|-(0)-[EBWebImageView:0x84cafa40]   (Names: '|':UIView:0x84caf9a0 )>",
"<NSLayoutConstraint:0x84cb30e0 V:[EBWebImageView:0x84cafa40]-(0)-[UILabel:0x84cb03a0'Kimberly Otte | Jenna Lau...']>",
"<NSLayoutConstraint:0x84cb3170 V:[UILabel:0x84cb03a0'Kimberly Otte | Jenna Lau...']-(8)-[UIImageView:0x84cb0ba0]>",
"<NSLayoutConstraint:0x84cb3260 V:[UIImageView:0x84cb0ba0]-(8)-[UIImageView:0x84cb0e20]>",
"<NSLayoutConstraint:0x84cb33b0 V:[UIImageView:0x84cb0e20]-(8)-[UILabel:0x84cb1540'Event Details']>",
"<NSLayoutConstraint:0x84cb34a0 V:[UILabel:0x84cb1540'Event Details']-(5)-[UITableView:0x7c361e00]>",
"<NSLayoutConstraint:0x84cb34d0 V:[UITableView:0x7c361e00]-(0)-|   (Names: '|':UIView:0x84caf9a0 )>",
"<NSLayoutConstraint:0x84cb54e0 H:[UIView:0x84caf9a0]-(0)-|   (Names: '|':UIView:0x84caf900 )>",
"<NSLayoutConstraint:0x84cb5510 V:|-(0)-[UIView:0x84caf9a0]   (Names: '|':UIView:0x84caf900 )>",
"<NSLayoutConstraint:0x84cb5540 H:|-(0)-[UIView:0x84caf9a0]   (Names: '|':UIView:0x84caf900 )>",
"<NSIBPrototypingLayoutConstraint:0x84cb55a0 'IB auto generated at build time for view with fixed frame' V:|-(581)-[UILabel:0x84cb37e0'Friends']   (Names: '|':UIView:0x84caf900 )>",
"<NSLayoutConstraint:0x84cb6c20 V:[UIView:0x84caf9a0]-(8)-[UILabel:0x84cb37e0'Friends']>",
"<NSLayoutConstraint:0x7fc3cb90 'UIView-Encapsulated-Layout-Width' H:[UIScrollView:0x860ed080(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7d237f80 h=-&- v=-&- UIView:0x84caf900.width == UIScrollView:0x860ed080.width>"

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x84cb19a0 V:[UITableView:0x7c361e00(225)]>
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.

如果还需要什么,请告诉我,谢谢你的帮助。

想明白了。NSIBPrototypingLayoutConstraints是由IB自动生成的,因为我没有在IB中设置它们。我只是在IB中向XIB添加了所有约束,选中了"在构建时删除"框中我想要更改的约束,一切都很顺利。我开始喜欢Auto Layout,因为它的解决方案简洁、优雅,而且比摆弄框架干净得多。

最新更新