经过多次测试,只有10.0以上版本才能修改Frame 不然会崩溃
- (void)layoutSubviews
{
[super layoutSubviews];
// if(kDevice_System >= 10.0) {
//
// NSString *content = self.titleLabel.text;
// UIFont *font = self.titleLabel.font;
// CGSize size = CGSizeMake(MAXFLOAT, self.height);
// CGSize buttonSize = [content boundingRectWithSize:size
// options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
// attributes:@{ NSFontAttributeName:font}
// context:nil].size;
//
// self.width = buttonSize.width + FitValue(70);
//
// }
// 调整图片
self.imageView.x = FitValue(5);
self.imageView.y = FitValue(10);
self.imageView.width = FitValue(20);
self.imageView.height = self.imageView.width;
// 调整文字
self.titleLabel.x = self.imageView.width + FitValue(15);
self.titleLabel.y = 0;
self.titleLabel.width = self.width - self.imageView.width - FitValue(10);
self.titleLabel.height = self.height;
}
不能修改,那就直接更新约束,放在重写title方法中,更节能
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
[super setTitle:title forState:state];
[self mas_updateConstraints:^(MASConstraintMaker *make) {
UIFont *font = self.titleLabel.font;
CGSize size = CGSizeMake(MAXFLOAT, self.height);
CGSize buttonSize = [title boundingRectWithSize:size
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{ NSFontAttributeName:font}
context:nil].size;
make.width.equalTo(@(buttonSize.width + FitValue(60)));
}];
}
网友评论