button设置图片,文字,系统适配

[self.visitorNumberButton setTitle:[NSString stringWithFormat:@"%ld人",(unsigned long)visitorCount] forState:UIControlStateNormal];
[self.visitorNumberButton setImage:[UIImage imageNamed:@"gameDetail_relate_right_arrow"] forState:UIControlStateNormal];
[self.visitorNumberButton.titleLabel sizeToFit];
//强制label宽度自适应(9.2系统会出现问题,一步步执行找出bug,展示太快,来不及获取button的label宽度,所以需要强制自适应)
CGFloat visitorLabelWidth = self.visitorNumberButton.titleLabel.frame.size.width + 4;
CGFloat visitorImageWidth = self.visitorNumberButton.imageView.frame.size.width + 4;
self.visitorNumberButton.titleEdgeInsets = UIEdgeInsetsMake(0, -visitorImageWidth, 0, visitorImageWidth);
self.visitorNumberButton.imageEdgeInsets = UIEdgeInsetsMake(0, visitorLabelWidth, 0, -visitorLabelWidth);

你可能感兴趣的:(button设置图片,文字,系统适配)