以编程方式自动布局不垂直居中
Programmatically autolayout not centering vertically
我想使用自动版式将视图垂直和水平居中,但不知何故我在这样做时遇到了麻烦。我在 iOS8 模拟器和 iOS8 设备上 运行。我尝试了两个版本的代码,其中 none 有效。以下是片段:
首先让我们分配我们正在使用的视图:
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];
对我来说似乎是一段完全合法的代码。但是没有。
尝试#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 中得到的结果的图片。
我该如何解决?
你不断地重新定义constraint
。你需要像 xconstraint
、yconstraint
、wconstraint
、hconstraint
这样的东西。然后将每个约束添加到 self.view 或 [self.view addConstraints:@[xconstraint, yconstraint,...];
您可能还需要致电 [self.view layoutIfNeeded];
更新
Y 约束的乘数需要为 1.0,而不是 0.5。
在设置垂直约束时,您将乘数设置为 0.5f,这是造成这种情况的原因。将其设置为 1.0f,就像您对水平约束所做的那样。
constraint = [NSLayoutConstraint constraintWithItem:inputView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
我想使用自动版式将视图垂直和水平居中,但不知何故我在这样做时遇到了麻烦。我在 iOS8 模拟器和 iOS8 设备上 运行。我尝试了两个版本的代码,其中 none 有效。以下是片段:
首先让我们分配我们正在使用的视图:
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];
对我来说似乎是一段完全合法的代码。但是没有。
尝试#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 中得到的结果的图片。
我该如何解决?
你不断地重新定义constraint
。你需要像 xconstraint
、yconstraint
、wconstraint
、hconstraint
这样的东西。然后将每个约束添加到 self.view 或 [self.view addConstraints:@[xconstraint, yconstraint,...];
您可能还需要致电 [self.view layoutIfNeeded];
更新
Y 约束的乘数需要为 1.0,而不是 0.5。
在设置垂直约束时,您将乘数设置为 0.5f,这是造成这种情况的原因。将其设置为 1.0f,就像您对水平约束所做的那样。
constraint = [NSLayoutConstraint constraintWithItem:inputView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];