ios change constraint programmatically

限于喜欢 提交于 2019-12-03 16:52:09

Well, if your 50 px for landscape and 100 for portrait can somehow be derived by relations to sibling subviews and fixed offsets to the bounds of superview, then yes, this can be done "at design time", and that's actually what auto layout is meant to be for.

Otherwise you would need to have a reference to the constraint, then in willRotateToInterfaceOrientation:duration: method you need to change the value of the constant of this constraint and call layoutIfNeeded for the superview.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    self.myConstraint.constant = (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) ? 50. : 100.;
    [self.someView layoutIfNeeded];
}

(written in the browser, might have mistakes)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!