使用Autolayout时在Y轴上移动UIButton

时间:2015-03-14 09:13:13

标签: ios objective-c uibutton

我正在开发一个项目,我正在为该应用程序使用自动布局/通用故事板。

我有一个UIButton,我想转到某个特定事件的新位置。但是不影响汽车布局。所以每台设备看起来都一样。

我设法通过此代码移动按钮:

int deltaY = 100;

    [ans1 removeConstraints:ans1.constraints];
    ans1.frame = CGRectMake(ans1.frame.origin.x, ans1.frame.origin.y + deltaY, ans1.frame.size.width, ans1.frame.size.height);

然而,按钮会在一秒左右的时间内移回原来的位置。

1 个答案:

答案 0 :(得分:4)

为什么要删除按钮中的约束?而不是删除约束,您可以创建按钮Y约束&的IBOutlet。然后改变Y Constrain值

选择要更改的LayoutConstraint,然后将其拖动到.h文件

Select the LayoutConstraint you want to change, then control drag it to your .h file

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *yAxisConstrain;

像这样改变你的Constrain值,

yAxisConstrain.constant = 0; //What ever the value you want.
[self.view layoutIfNeeded];
相关问题