今天在自定義button時刷袍,遇到了關(guān)于layoutSubviews方法調(diào)用的問題米碰,由于使用的是sizeTofit自適應(yīng)的方法谷丸,所以初始化時并沒有給按鈕設(shè)置frame堡掏,,只是在layout方法里改變了titleLable和imageView的位置刨疼,按鈕卻能正常顯示泉唁,就想到什么時候走了layout方法。
- (void)layoutSubviews{
[super layoutSubviews];
NSLog(@"layout1");
self.backgroundColor = [UIColor redColor];
self.titleLabel.frame = CGRectMake(self.imageView.frame.origin.x, self.titleLabel.frame.origin.y, self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);
self.imageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame), self.imageView.frame.origin.y, self.imageView.frame.size.width, self.imageView.frame.size.height);
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state{
[super setTitle:title forState:state];
NSLog(@"settitle");
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state{
NSLog(@"setimage");
[super setImage:image forState:state];
[self sizeToFit];
}
- 何時使用layout