Change NSLayoutConstraint constant not working in layoutSubviews

两盒软妹~` 提交于 2019-12-24 10:39:26

问题


I am trying to change a UIButton's width when the view animates to landscape mode. But the method is called because I set a break point there, but the button's width doesn't change. I add a IBOutlet constraint to button's width named: globalButtonWidthConstraint.

My current code :

- (void)layoutSubviews {
    [super layoutSubviews];

    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    CGFloat screenH = screenSize.height;
    CGFloat screenW = screenSize.width;
    BOOL isLandscape =  !(self.frame.size.width == (screenW*(screenW<screenH))+(screenH*(screenW>screenH)));

    if (isLandscape) {
        self.globalButtonWidthConstraint.constant = 100;
        [self layoutIfNeeded];
    } else {
        self.globalButtonWidthConstraint.constant = 47;
        [self layoutIfNeeded];
    }
}

回答1:


Try updating constraint constant in "viewDidLayoutSubviews".

 -(void)viewDidLayoutSubviews
{

   [super viewDidLayoutSubviews];

  // Update constraint constant

   [self.view layoutSubviews];

}

It worked for me.

EDIT: Make sure that there is no other constraints that conflicting this width constraint.



来源:https://stackoverflow.com/questions/27288507/change-nslayoutconstraint-constant-not-working-in-layoutsubviews

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