0

我创建了一个简单的项目来以编程方式使用约束移动一些组件,在这种情况下它只是一个 UIPickerView,但不幸的是我总是得到错误代码,我真的不明白它的含义是什么。

这是我的界面:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;

@end

这是我的实现:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLayoutConstraint *constraint = [NSLayoutConstraint
                                      constraintWithItem:_pickerView
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeTop
                                      multiplier:1.0f
                                      constant:216.f];    
    [self.view addConstraint:constraint];    
}

这是我在调试器控制台上看到的:

2013-09-19 10:44:19.582 Constrain2[3092:c07] 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:0x71808f0 V:[UIPickerView:0x7180fd0(216)]>",
    "<NSLayoutConstraint:0x71805d0 V:|-(216)-[UIPickerView:0x7180fd0]   (Names: '|':UIView:0x7181290 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7184d50 h=--& v=--& V:[UIView:0x7181290(416)]>",
    "<NSLayoutConstraint:0x7181740 UIPickerView:0x7180fd0.bottom == UIView:0x7181290.bottom>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x71808f0 V:[UIPickerView:0x7180fd0(216)]>

为什么我有这样的错误信息?我还尝试了本教程http://ioscreator.com/auto-layout-in-ios-6-adding-constraints-through-code/,我发现 UIButton 没有问题。但是,它不是使用情节提要来创建按钮。而在我的情况下,我通过情节提要放置 UIPickerView 。这是造成问题的原因吗?

谢谢你。

4

1 回答 1

1

约束 0x71808f0 想要将选取器的高度设置为 216 点。

约束 0x71805d0 想要将选取器的顶部边缘设置为比self.view. (这是您在 中添加的约束viewDidLoad。)

约束 0x7181740 想要将选取器视图的底部边缘设置为正好位于self.view.

自动布局只能通过将高度设置self.view为 216 + 216 = 432 点来满足这三个约束。

不幸的是,约束 0x7184d50 想要将高度设置self.view为 416 点。因此自动布局不能同时满足所有约束。

我猜这self.view被限制为 416 点高,因为您使用的是 3.5 英寸屏幕(iPhone 4S 或更早版本),高度为 480 点,并且您打开了状态栏(20 点)和导航条或工具栏(44 分),剩下 480 - 20 - 44 = 416 分用于视图。

为什么要在中添加约束viewDidLoad

于 2013-09-19T04:16:15.030 回答