我正在使用带有约束的自动版式。 superview的顶部空间为100
我现在想更改此设置,以便当用户将设备旋转到横向时 superview的顶部空间是50
这可以在设计时完成吗?
这如何在运行时完成?
最佳答案
好吧,如果您的 50
横向 px 和 100
纵向可以通过与同级 subview 的关系和父 View 边界的固定偏移以某种方式导出,那么是的,这可以在“设计时”完成,这实际上就是自动布局的目的。
否则您需要引用约束
,然后在willRotateToInterfaceOrientation:duration:
方法中您需要更改constant的值此
并为 super View 调用约束
的layoutIfNeeded
。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
self.myConstraint.constant = (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) ? 50. : 100.;
[self.someView layoutIfNeeded];
}
(浏览器写的,可能有错误)
关于ios 以编程方式更改约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542626/