
14-16012Z93F2E0.jpg
先看效果

Untitled.gif
iOS系统提供给我的button是图片在左文字在右有时候不能满足我们的需要,这个时候就要自定义一个button
自定义一个button,GHAdjustmentButton继承GHButton
在内部重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
return self;
}
imageView图片填充方式选择UIViewContentModeScaleAspectFit选择等比例缩放
接下来重写layoutSubviews方法,这个方法每次子控件重新布局都会被调用
- (void)layoutSubviews {
[super layoutSubviews];
}
UIEdgeInsetsMake(CGFloat top, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>) 是指控件指定方向的偏移量,依次是上,左,下,右
首先固定imageView的位置
self.imageEdgeInsets = UIEdgeInsetsMake(0, self.width - self.imageView.width, 0, 0);
在设定titleLabel的位置
self.titleEdgeInsets = UIEdgeInsetsMake(0, self.width - margin - self.imageView.width -self.titleLabel.width - self.imageView.width , 0,0);
最后完整代码
- (void)layoutSubviews {
[super layoutSubviews];
/** 设置imageViewframe */
self.imageView.frame = CGRectMake(self.width - 15, 3, 15, 15);
/** 距离屏幕右侧距离 */
CGFloat margin = 15;
/** 上偏移0 左偏移整体宽度 */
self.imageEdgeInsets = UIEdgeInsetsMake(0, self.width - self.imageView.width, 0, 0);
self.titleEdgeInsets = UIEdgeInsetsMake(0, self.width - margin - self.imageView.width -self.titleLabel.width - self.imageView.width , 0,0);
}
网友评论