gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:38:18 38 4
gpt4 key购买 nike

我正在尝试向XIB中的UIViewcontentContainer添加一些约束。我对“自动布局”很满意,但是新的iPhone迫使我进入了“自动布局”。不好意思,但是我没有10个声誉,所以堆栈不会让我发布XIB的图片,所以解释起来会更加复杂...
contentContainer中有一个称为UIViewfixedContentContainer。就其内部所有UIViews的高度而言,该容器每次都是相同的。我在IB中对该容器的内容添加了约束。那里一切正常。在fixedContentContainer中的contentContainer下方也有4个UIView对象:

  • 称为UILabel的静态friendsLabel
  • 称为UITableViewtableView
  • 称为UILabel的静态eventDescriptionHeaderLabel
  • 动态UILabel,称为eventDescriptionLabel

  • 我要完成的事情很简单。如果用户打开一个事件并且没有 friend 去,我会隐藏 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中,选中了“在构建时删除”框,以查找要更改的约束,所有操作都从此顺利进行。我开始喜欢自动版式,因为该解决方案最终变得简短,优雅,而且比弄乱框架更干净。

    关于ios - 使用自动布局隐藏UIView时以编程方式添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26395936/

    38 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com