iOS View執(zhí)行順序

@interface BSTTextPromtView ()

/** <#statements#> */
@property (nonatomic, strong) UIView *lineView;

/** <#statements#> */
@property (nonatomic, strong) UIImageView *iconImageView;

/** <#statements#> */
@property (nonatomic, strong) UILabel *textLabel;
@end

@implementation BSTTextPromtView

- (instancetype)initWithFrame:(CGRect)frame
{
    NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
    if (self) {
        [self initUI];
    }
    return self;
}


- (void)initUI {
    NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.lineView];
    [self addSubview:self.iconImageView];
    [self addSubview:self.textLabel];
}



- (void)layoutSubviews {
    NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
    [super layoutSubviews];
    self.lineView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 0.5);
    self.iconImageView.frame = CGRectMake(32, 12, 14, 14);
    UIFont *textFont = [UIFont systemFontOfSize:11.0];
    CGFloat textHeight = [self.textLabel.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame)- 50-32, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textFont} context:nil].size.height;
    self.textLabel.frame = CGRectMake(50, 12, CGRectGetWidth(self.frame)- 50-32, textHeight);
}


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
}

- (void)updateConstraints {
    NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
    // 蘋果官方說(shuō)法:Call `[super update<wbr>Constraints]` as the final step in your implementation.
   [super updateConstraints];
}

- (void)setImage:(UIImage *)image {
    _image = image;
    self.iconImageView.image = image;
}

- (void)setText:(NSString *)text {
    _text = text;
    self.textLabel.text = text;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor = [UIColor colorWithRed:4/255.0 green:4/255.0 blue:22/255.0 alpha:0.15];
    }
    return _lineView;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _iconImageView.image = [UIImage imageNamed:@"icon_properties"];
    }
    return _iconImageView;
}

- (UILabel *)textLabel {
    if (!_textLabel) {
        NSString *text = @"該內(nèi)容來(lái)自第三方網(wǎng)站分享,非公司官方觀點(diǎn),";
        UIFont *textFont = [UIFont systemFontOfSize:11.0];
        _textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _textLabel.text = text;
        _textLabel.numberOfLines = 0;
        _textLabel.textColor = [UIColor colorWithRed:164/255.0 green:160/255.0 blue:168/255.0 alpha:1.0];
        _textLabel.font = textFont;
    }
    return _textLabel;
}
@end

從xib加載打印如下:

2020-11-05 11:30:58.038243+0800 aa[81499:784826] -[BSTTextPromtView initWithCoder:] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.038399+0800 aa[81499:784826] -[BSTTextPromtView initUI] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.060203+0800 aa[81499:784826] -[BSTTextPromtView updateConstraints] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.060876+0800 aa[81499:784826] -[BSTTextPromtView layoutSubviews] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.064591+0800 aa[81499:784826] -[BSTTextPromtView drawRect:] frame:{{57, 204}, {309, 65}}

可以看到updateConstraints > layoutSubviews>drawRect
其中顯示調(diào)用方法為

// updateConstraints:
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
// layoutSubviews:
[self setNeedsLayout];
[self layoutIfNeeded];
// drawRect: If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
[self setNeedsDisplay];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鸭蛙,一起剝皮案震驚了整個(gè)濱河市赎败,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌眷柔,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異洗做,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)彰居,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門诚纸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人陈惰,你說(shuō)我怎么就攤上這事畦徘。” “怎么了抬闯?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵井辆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我溶握,道長(zhǎng)杯缺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任睡榆,我火速辦了婚禮萍肆,結(jié)果婚禮上袍榆,老公的妹妹穿的比我還像新娘。我一直安慰自己塘揣,他們只是感情好包雀,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著亲铡,像睡著了一般才写。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上奖蔓,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天赞草,我揣著相機(jī)與錄音,去河邊找鬼锭硼。 笑死房资,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的檀头。 我是一名探鬼主播轰异,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼暑始!你這毒婦竟也來(lái)了搭独?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤廊镜,失蹤者是張志新(化名)和其女友劉穎牙肝,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嗤朴,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡配椭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雹姊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片股缸。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖吱雏,靈堂內(nèi)的尸體忽然破棺而出敦姻,到底是詐尸還是另有隱情,我是刑警寧澤歧杏,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布镰惦,位于F島的核電站,受9級(jí)特大地震影響犬绒,放射性物質(zhì)發(fā)生泄漏旺入。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一凯力、第九天 我趴在偏房一處隱蔽的房頂上張望眨业。 院中可真熱鬧急膀,春花似錦、人聲如沸龄捡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)聘殖。三九已至,卻和暖如春行瑞,著一層夾襖步出監(jiān)牢的瞬間奸腺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工血久, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留突照,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓氧吐,卻偏偏與公主長(zhǎng)得像讹蘑,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子筑舅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359