search

Home  >  Q&A  >  body text

ios - 无法用代码获取到NSLayoutAttributeTrailing的值为什么?

我在storyboard中使用了autolayout,因此不能通过更改frame的值来更改控件的位置,使用如下方法:

-(void)replaceView:(UIView *)view constrainWithConstant:(CGFloat)constant attribute:(NSLayoutAttribute)attribute{

for (NSLayoutConstraint *constraint in view.superview.constraints) {
    if (constraint.firstItem == view && constraint.firstAttribute == attribute) {
        constraint.constant = constant;
    }
}

}
可以获取到NSLayoutAttributeLeading以及NSLayoutAttributeTop的值,并在代码中更改。但是无法获取到NSLayoutAttributeTrailing的值,请问我该如何用代码获取到NSLayoutAttributeTrailing的值并加以更改呢?

天蓬老师天蓬老师2773 days ago733

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 17:11:36

    If trailing is set up in the storyboard, you should be able to get it (notice that you only judged the firstAttribute, maybe it is a problem with the order of adding it during the connection? Check the second and try it), if it is not set, just add it, no need to replace

    reply
    0
  • Cancelreply