以编程方式自动布局不垂直居中

时间:2015-04-09 19:04:14

标签: ios objective-c uiview ios8 autolayout

我想使用autolayout垂直和水平居中视图,但不知何故,我遇到了麻烦。我在iOS8模拟器和iOS8设备上运行。有两个版本的代码,我尝试过,但没有一个工作。以下是片段:

首先让我们分配我们正在使用的视图:

UIView *inputView = UIView.new; //Don't hate me for the dot syntax :)
inputView.translatesAutoresizingMaskIntoConstraints = NO;
inputView.backgroundColor = UIColor.redColor;

[self.view addSubview:inputView];

尝试#1:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]; //Center the view horizontally
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]; //And make it have the same width as the view
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:260.0f]; //Force exactly 260pts view height
[inputView addConstraint:constraint]; //If I add it to self.view it's the same thing

constraint = [NSLayoutConstraint constraintWithItem:inputView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.5f constant:0.0f]; //And try to center it vertically - DOES NOT WORK... wtf
[self.view addConstraint:constraint];

对我来说似乎是一段完全合法的代码。但是NOPE。

尝试#2。它的代码行数基本相同。

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[inputView(>=320)]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(inputView)];
[self.view addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[inputView(260)]-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(inputView)];
[self.view addConstraints:constraints];

现在这个也不起作用。我的意思是它确实水平居中视图,但是当我添加垂直约束时,我会收到一个警告,告诉我我在自动布局时很糟糕。它说它无法满足所有的限制。更具体地说:

(
    "<NSLayoutConstraint:0x17408be00 UIView:0x174197010.top == UIView:0x174196da0.topMargin>",
    "<NSLayoutConstraint:0x17408bdb0 V:[UIView:0x174197010(260)]>",
    "<NSLayoutConstraint:0x17408bd60 UIView:0x174196da0.bottomMargin == UIView:0x174197010.bottom>",
    "<NSLayoutConstraint:0x17008a000 'UIView-Encapsulated-Layout-Height' V:[UIView:0x174196da0(667)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17408bd60 UIView:0x174196da0.bottomMargin == UIView:0x174197010.bottom>

我不知道如何解决这个问题。以下是我从尝试#1和#2获得的结果图片。

Layout issues

我该如何解决?

2 个答案:

答案 0 :(得分:2)

在设置垂直约束时,您将乘数设为0.5f,这导致了这一点。将它设为1.0f就像你对水平约束一样。

constraint = [NSLayoutConstraint constraintWithItem:inputView
                                          attribute:NSLayoutAttributeCenterY
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:self.view
                                          attribute:NSLayoutAttributeCenterY
                                         multiplier:1.0f
                                           constant:0.0f];

答案 1 :(得分:1)

您不断重新定义constraint。您需要xconstraintyconstraintwconstrainthconstraint之类的内容。然后将每个约束添加到self.view或[self.view addConstraints:@[xconstraint, yconstraint,...];

您可能还需要致电[self.view layoutIfNeeded];

更新

Y约束的乘数需要为1.0,而不是0.5。

相关问题