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];

运行效果:

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


修改坐标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