>  Q&A  >  본문

ios - UIView重复调用多次layoutSubviews方法

上面这段代码是我自定义UIView里面的一个UILabel,因为我需要在Controller里面动态更新该UILabel的内容,所以我在自定义UIView里面重写了UILabel的getter方法。
现在问题来了,当我在Controller里面给UILabel赋值的时候,却导致了一个严重的问题,自定义UIView一直在重复调用若干次layoutSubviews这个方法。非常郁闷,不知道哪里出了问题。
希望各位能给我指条明路,解决了很久没找出问题所在。感谢

- (UILabel *)moneyLabel
{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc] init];
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        [_moneyLabel setTextColor:MAIN_COLOR];
        [_moneyLabel setFont:[HDComponentTools regularFont:30]];
        self.moneyLabel = [HDComponentTools labelSizeFit:self.moneyLabel];
        [self addSubview:_moneyLabel];
        
        [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_moneyTitle.mas_bottom).offset(5);
            make.centerX.equalTo(_moneyTitle.mas_centerX);
        }];
    }
    return _moneyLabel;
}
ringa_leeringa_lee2711일 전633

모든 응답(2)나는 대답할 것이다

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:27:36

    layoutSubviews는 uiview의 프레임이 변경될 때 호출됩니다. 이는 제약 조건을 사용하여 uilabel 크기를 조정하는 경우 발생합니다.

    회신하다
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:27:36

    LayoutSubView는 일반적으로 프레임을 직접 작성합니다

    회신하다
    0
  • 취소회신하다