调整UIButton中的imageView和titleLabel的相对位置

UIButton中默认图片在左边,标题在右边,如果想任意调整这两个子控件的位置有两种方式:
方式一:通过调整子控件的【边缘内边距】edgeInset来实现:imageEdgeInsets 、titleEdgeInsets ;
方式二:自定义按钮,在layoutSubview方法中直接调整子控件的坐标;


方式一代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(10, 100, 90, 30);
[button setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
[button setTitle:@"下载" forState:UIControlStateNormal];
[self.view addSubview:button];

// UIEdgeInset: top, left, bottom, right 
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.backgroundColor = [UIColor grayColor];
button2.frame = CGRectMake(10, 150, 90, 30);
[button2 setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
[button2 setTitle:@"下载2" forState:UIControlStateNormal];
button2.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -90);
button2.titleEdgeInsets = UIEdgeInsetsMake(0, -70, 0, 0);
[self.view addSubview:button2];


UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.backgroundColor = [UIColor grayColor];
button3.frame = CGRectMake(10, 200, 80, 80);
[button3 setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
[button3 setTitle:@"下载3" forState:UIControlStateNormal];
button3.imageEdgeInsets = UIEdgeInsetsMake(-30, 15, 0, 0);
button3.titleEdgeInsets = UIEdgeInsetsMake(0, -40, -30, 0);
[self.view addSubview:button3];

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
button4.backgroundColor = [UIColor grayColor];
button4.frame = CGRectMake(10, 300, 80, 80);
[button4 setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
[button4 setTitle:@"下载4" forState:UIControlStateNormal];
button4.imageEdgeInsets = UIEdgeInsetsMake(30, 15, 0, 0);
button4.titleEdgeInsets = UIEdgeInsetsMake(-20, -30, 0, 0);
[self.view addSubview:button4];

方式二代码:直接修改子控件的坐标(x,y)

#import "XXButton.h"
@implementation XXButton
- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageView.frame = CGRectMake(30, 10, self.imageView.frame.size.width, self.imageView.frame.size.height);

    self.titleLabel.frame = CGRectMake(10, 55, self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);
    [self.titleLabel sizeToFit];30);
}

@end
XXButton *button5 = [XXButton buttonWithType:UIButtonTypeCustom];
button5.backgroundColor = [UIColor grayColor];
button5.frame = CGRectMake(10, 450, 110, 90);
[button5 setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
[button5 setTitle:@"自定义按钮" forState:UIControlStateNormal];
[self.view addSubview:button5];

运行效果:
这里写图片描述


修改坐标x,y调整相对位置:

#import "BWFastLoginButton.h"
#import "UIViewExt.h"

@implementation BWFastLoginButton
- (void)layoutSubviews {
    [super layoutSubviews];

    // 设置图片坐标
    self.imageView.top = 0;
    self.imageView.centerX = self.width * 0.5;

    // 设置标题坐标
    self.titleLabel.top = self.height - self.titleLabel.height;
    [self.titleLabel sizeToFit];
    self.titleLabel.centerX = self.width * 0.5;

}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风流 少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值