UIAlertView导致其位置在运行时更改的UIButton返回到原始位置

时间:2013-11-21 16:50:17

标签: ios

所以我有一个storyboard autolayout(我需要它用于其他视图,而不是这个视图,但我不能只为一个视图关闭autolayout)。我在viewDidLayoutSubviews中更改了两个UI元素的位置。  然后我点击其中一个UI元素,显示UIAlertView,这会导致此UI元素返回其原始位置(在故事板中指定)。但是(!)其他元素保持不变。我试图实现UIAlertViewDelegate把按钮放回去 - willPresentAlertView

- didPresentAlertView

- alertView:willDismissWithButtonIndex

- alertView:didDismissWithButtonIndex

), 但它不起作用(适用于didPresentAlertView,但没有任何解雇)。 有没有办法让按钮保持原位并保持autolayout

1 个答案:

答案 0 :(得分:0)

您需要更改约束而不是正在移动的视图的框架。将Outlet添加到要更改的约束(x或y位置),您需要更新viewDidLayoutSubviews方法中的约束。

当显示UIAlertView时,会出现一个动画,它会重置视图中的帧。由于您没有首先更改约束(只是视图的框架),因此它会被带回原始位置。

如果您有约束:

// .h file

/// Hooked up to your Storyboard file
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myConstraint;

// .m file

self.myConstraint.constant = 300; //Your new position in either x or y.