在 Y 轴上移动 UIButton
Moving UIButton on Y Axis
我的项目中有几个 UIButton,但我希望能够通过代码移动它们在 Y 轴上的位置。而且,通过代码移动按钮会影响自动布局/通用故事板放置吗?
谢谢:)
如果你使用 AutoLayout
你可以使用这样的东西:
-(void)setConstraintsYPosition:(CGFloat)yPosition forButton:(UIButton*)button withConstraint:(NSLayoutConstraint*)constraint
{
constraint.constant = yPosition;
[self.view needsUpdateConstraints];
[self.view layoutIfNeeded];
[button setNeedsDisplay];
}
并且不要忘记为您想要的约束创建一个 IBOutlet change/pass
如果您使用的是自动版式,请在将新框架应用于按钮之前移除约束:
[button removeConstraints:button.constraints];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y + deltaY, button.frame.size.width, button.frame.size.height);
我的项目中有几个 UIButton,但我希望能够通过代码移动它们在 Y 轴上的位置。而且,通过代码移动按钮会影响自动布局/通用故事板放置吗?
谢谢:)
如果你使用 AutoLayout
你可以使用这样的东西:
-(void)setConstraintsYPosition:(CGFloat)yPosition forButton:(UIButton*)button withConstraint:(NSLayoutConstraint*)constraint
{
constraint.constant = yPosition;
[self.view needsUpdateConstraints];
[self.view layoutIfNeeded];
[button setNeedsDisplay];
}
并且不要忘记为您想要的约束创建一个 IBOutlet change/pass
如果您使用的是自动版式,请在将新框架应用于按钮之前移除约束:
[button removeConstraints:button.constraints];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y + deltaY, button.frame.size.width, button.frame.size.height);