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

查看:448
本文介绍了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;
}

解决方案

layoutSubviews是在uiview的frame发生变动时调用,你用约束自适应uilabel大小当然会这样。

这篇关于ios - UIView重复调用多次layoutSubviews方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆