首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用约束以编程方式移动UIPickerView,但始终得到此错误消息

尝试使用约束以编程方式移动UIPickerView,但始终得到此错误消息
EN

Stack Overflow用户
提问于 2013-09-19 03:49:52
回答 1查看 1K关注 0票数 0

我创建了一个简单的项目,通过编程方式移动一些组件,在这种情况下,它只是一个UIPickerView,但不幸的是,我总是得到错误代码,我真的不明白它的意义。

这是我的界面:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

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

@end

以下是我的实现:

代码语言:javascript
复制
- (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];    
}

下面是我在调试器控制台上看到的内容:

代码语言:javascript
复制
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放在一起。这就是问题的原因吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-19 04:16:15

约束0x71808f0希望将选择器的高度设置为216点。

约束0x71805d0希望将选择器的上边缘设置为self.view顶部边缘之下的216点。(这是您在viewDidLoad中添加的约束。)

约束0x7181740希望将选择器视图的底部边缘设置为完全位于self.view的底部边缘。

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

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

我猜self.view被限制为416点高,因为你在3.5英寸屏幕上(iPhone 4S或更早),屏幕高度为480点,状态条打开(20点),导航条或工具栏(44点)打开,留下48-20点= 416点供查看。

为什么要在viewDidLoad中添加约束

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18885891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档