iOS UIButton 图文上下布局
2018-03-19 本文已影响88人
冷煖自知
继承UIButton类创建
在.m中添加方法
- (void)layoutButtonWithImageTitleSpace:(CGFloat)space {
CGFloat imageWith = self.imageView.image.size.width;
CGFloat imageHeight = self.imageView.image.size.height;
CGFloat labelWidth = self.titleLabel.intrinsicContentSize.width;
CGFloat labelHeight = self.titleLabel.intrinsicContentSize.height;
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
self.titleEdgeInsets = labelEdgeInsets;
self.imageEdgeInsets = imageEdgeInsets;
}
在需要的时候调用后,Button会改为上下布局