ios6 自动布局约束

Posted

技术标签:

【中文标题】ios6 自动布局约束【英文标题】:ios6 autolayout constraints 【发布时间】:2013-02-21 23:45:58 【问题描述】:

我试图弄清楚如何在 UIViewController 上使用约束(在 UINavigationController 内的 UITabBarController 内)... 一千多字,这就是我想做的……

实际上,第一个是 100px 高度,无论方向如何,第二个是一个广告(纵向 50px,横向 32px),最后是一个 tableView 将占据其余部分。

到目前为止,这是我的代码:

PMMainProfilHeader  *profilHeader = [[PMMainProfilHeader alloc] init];
    [self.view addSubview:profilHeader];
    [profilHeader release];

    UIView *adTest = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    adTest.backgroundColor = [UIColor redColor];
    [self.view addSubview:adTest];
    [adTest release];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:_tableView];
    [_tableView release];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[profilHeader]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(profilHeader)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[adTest]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(adTest)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_tableView]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(_tableView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[profilHeader]-0-[adTest]-0-[_tableView]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(profilHeader, adTest, _tableView)]];

但当然它不起作用......而且我不知道为什么,当我恢复调试几次时,我从调试器中得到了这条评论:

2013-02-22 00:17:13.207 MyApplication[6822:907] 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:0x1d0b20d0 H:[UIView:0x1d0ae7e0]-(0)-|   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1d0b2090 H:|-(0)-[UIView:0x1d0ae7e0]   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1d0b2000 H:[PMMainProfilHeader:0x1d09f5b0]-(0)-|   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1d0b2320 H:|-(0)-[PMMainProfilHeader:0x1d09f5b0]   (Names: '|':UIView:0x1c596de0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5dcb10 h=--& v=--& UIView:0x1d0ae7e0.midX ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5daf80 h=--& v=--& PMMainProfilHeader:0x1d09f5b0.midX == + 160>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1d0b20d0 H:[UIView:0x1d0ae7e0]-(0)-|   (Names: '|':UIView:0x1c596de0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-02-22 00:17:41.706 MyApplication[6822:907] 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:0x1c5ab460 V:[PMMainProfilHeader:0x1d09f5b0]-(0)-[UIView:0x1d0ae7e0]>",
    "<NSLayoutConstraint:0x1c5ab3f0 V:|-(0)-[PMMainProfilHeader:0x1d09f5b0]   (Names: '|':UIView:0x1c596de0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5dcca0 h=--& v=--& UIView:0x1d0ae7e0.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5db050 h=--& v=--& PMMainProfilHeader:0x1d09f5b0.midY == + 50>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c5ab460 V:[PMMainProfilHeader:0x1d09f5b0]-(0)-[UIView:0x1d0ae7e0]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-02-22 00:17:43.382 MyApplication[6822:907] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1d0c0530 h=--& v=--& UITableView:0x1db44600.midX ==>",
    "<NSLayoutConstraint:0x1c5ab160 H:[UITableView:0x1db44600]-(0)-|   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1c5ab120 H:|-(0)-[UITableView:0x1db44600]   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1d0b2000 H:[PMMainProfilHeader:0x1d09f5b0]-(0)-|   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1d0b2320 H:|-(0)-[PMMainProfilHeader:0x1d09f5b0]   (Names: '|':UIView:0x1c596de0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5daf80 h=--& v=--& PMMainProfilHeader:0x1d09f5b0.midX == + 160>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c5ab160 H:[UITableView:0x1db44600]-(0)-|   (Names: '|':UIView:0x1c596de0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-02-22 00:17:45.290 MyApplication[6822:907] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1c5dcce0 h=--& v=--& V:[UIView:0x1d0ae7e0(0)]>",
    "<NSLayoutConstraint:0x1c5ab510 V:[UITableView:0x1db44600]-(0)-|   (Names: '|':UIView:0x1c596de0 )>",
    "<NSLayoutConstraint:0x1c5ab4b0 V:[UIView:0x1d0ae7e0]-(0)-[UITableView:0x1db44600]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5ed510 h=-&- v=-&- UILayoutContainerView:0x1c5d1440.height == UIWindow:0x1c564480.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5ef2e0 h=--- v=--- V:[UIWindow:0x1c564480(480)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5eb9e0 h=-&- v=-&- UINavigationTransitionView:0x1c5d2b20.height == UILayoutContainerView:0x1c5d1440.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e9f80 h=-&- v=-&- UIViewControllerWrapperView:0x1d0ba580.height == UINavigationTransitionView:0x1c5d2b20.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e8610 h=-&- v=-&- UILayoutContainerView:0x1d096c30.height == UIViewControllerWrapperView:0x1d0ba580.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e6ed0 h=-&- v=-&- UITransitionView:0x1d0999d0.height == UILayoutContainerView:0x1d096c30.height - 49>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e5980 h=-&- v=-&- UIViewControllerWrapperView:0x1c5cb7a0.height == UITransitionView:0x1d0999d0.height - 20>",
    "<NSAutoresizingMaskLayoutConstraint:0x1d0c0710 h=--& v=--& UITableView:0x1db44600.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e4270 h=-&- v=-&- UIView:0x1c596de0.height == UIViewControllerWrapperView:0x1c5cb7a0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5dcca0 h=--& v=--& UIView:0x1d0ae7e0.midY ==>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c5ab4b0 V:[UIView:0x1d0ae7e0]-(0)-[UITableView:0x1db44600]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-02-22 00:17:48.283 MyApplication[6822:907] 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:0x1c5ab510 V:[UITableView:0x1db44600]-(0)-|   (Names: '|':UIView:0x1c596de0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5ed510 h=-&- v=-&- UILayoutContainerView:0x1c5d1440.height == UIWindow:0x1c564480.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5ef2e0 h=--- v=--- V:[UIWindow:0x1c564480(480)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5eb9e0 h=-&- v=-&- UINavigationTransitionView:0x1c5d2b20.height == UILayoutContainerView:0x1c5d1440.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e9f80 h=-&- v=-&- UIViewControllerWrapperView:0x1d0ba580.height == UINavigationTransitionView:0x1c5d2b20.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e8610 h=-&- v=-&- UILayoutContainerView:0x1d096c30.height == UIViewControllerWrapperView:0x1d0ba580.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e6ed0 h=-&- v=-&- UITransitionView:0x1d0999d0.height == UILayoutContainerView:0x1d096c30.height - 49>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e5980 h=-&- v=-&- UIViewControllerWrapperView:0x1c5cb7a0.height == UITransitionView:0x1d0999d0.height - 20>",
    "<NSAutoresizingMaskLayoutConstraint:0x1d0c0710 h=--& v=--& UITableView:0x1db44600.midY ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x1c5e4270 h=-&- v=-&- UIView:0x1c596de0.height == UIViewControllerWrapperView:0x1c5cb7a0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x1d0c0750 h=--& v=--& V:[UITableView:0x1db44600(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c5ab510 V:[UITableView:0x1db44600]-(0)-|   (Names: '|':UIView:0x1c596de0 )>

感谢您阅读整个(巨大的)帖子!希望有人知道为什么它不起作用!

【问题讨论】:

Getting weird error talking about constraints in Xcode的可能重复 【参考方案1】:

好的,我找到了答案……

我只需要为我使用约束的每个视图调用 setTranslatesAutoresizingMaskIntoConstraints:

[profilHeader setTranslatesAutoresizingMaskIntoConstraints:NO];
[adTest setTranslatesAutoresizingMaskIntoConstraints:NO];
[_tableView setTranslatesAutoresizingMaskIntoConstraints:NO];

希望对某人有所帮助...

【讨论】:

我无法想象在代码中做这一切比在 Interface Builder 中做更有效率。它更容易设置,您可以获得有关发生情况的视觉反馈,当发生冲突时,您要么在 UI 中看到出现问题,要么有非常明确的警告或错误。你也可以通过这样做来防止 translatesAutoresizingMaskIntoConstraints。

以上是关于ios6 自动布局约束的主要内容,如果未能解决你的问题,请参考以下文章

ios 6自动布局约束错误

ios 6:使用自动布局约束垂直居中 UILabel

如何管理适用于 iOS 7 和 iOS 6.0 的自动布局约束

如何在启用自动布局的情况下更改 iOS6 中的 UIView 大小?

自动布局 iOS7 -> 带状态栏的 iOS6

具有动态视图数量和自动布局 iOS6 的 UIScrollView