在Y轴上移动UIButton

时间:2015-03-13 08:46:40

标签: ios objective-c uibutton position

我的项目中有一些UIButtons,但是我希望能够通过代码在Y轴上移动它们的位置。而且,移动按钮槽代码会影响自动布局/通用故事板放置吗?

谢谢:)

2 个答案:

答案 0 :(得分:0)

如果您使用AutoLayout,可以使用以下内容:

-(void)setConstraintsYPosition:(CGFloat)yPosition forButton:(UIButton*)button withConstraint:(NSLayoutConstraint*)constraint
{
    constraint.constant = yPosition;
    [self.view needsUpdateConstraints];
    [self.view layoutIfNeeded];
    [button setNeedsDisplay];
}

并且不要忘记为要更改/传递的约束创建IBOutlet

答案 1 :(得分:0)

如果您使用的是自动布局,请在将新框架应用于按钮之前删除约束:

[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);
相关问题