#UIButton#自定义UIButton

原生UIButton创建:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 150, 50);
    button.backgroundColor = [UIColor yellowColor];
    // 注意:下面这种方式设置按钮文字没有效果
    // button.titleLabel.text = @"普通按钮";
    [button setTitle:@"普通按钮" forState:UIControlStateNormal];
    button.titleLabel.backgroundColor = [UIColor purpleColor];
    [button setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_normal"] forState:UIControlStateNormal];
    // 注意:下面这种方式设置按钮文字没有效果
    // button.imageView.image = [UIImage imageNamed:@"miniplayer_btn_playlist_normal"];
    button.imageView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:button];
} 
  • 效果:


    #UIButton#自定义UIButton_第1张图片
    Snip20160823_4.png

自定义Button

  • 创建一个LHLUIButton类,继承UIButton
#UIButton#自定义UIButton_第2张图片
Snip20160823_5.png
  • .m文件中
// 方法一:重写下面两个方法 重新设置子控件的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    return CGRectMake(0, 0, 100, 50);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    return CGRectMake(100, 0, 50, 50);
}
  • 在ViewController中导入LHLUIButton,用LHLUIButton创建按钮
#import "ViewController.h"
#import "LHLButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    LHLButton *button = [LHLButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 150, 50);
    button.backgroundColor = [UIColor yellowColor];
    // 注意:下面这种方式设置按钮文字没有效果
    // button.titleLabel.text = @"普通按钮";
    [button setTitle:@"普通按钮" forState:UIControlStateNormal];
    button.titleLabel.backgroundColor = [UIColor purpleColor];
    [button setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_normal"] forState:UIControlStateNormal];
    // 注意:下面这种方式设置按钮文字没有效果
    // button.imageView.image = [UIImage imageNamed:@"miniplayer_btn_playlist_normal"];
    button.imageView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:button];
}
  • 效果图:


    #UIButton#自定义UIButton_第3张图片
    Snip20160823_6.png
  • 重写initWithFrame方法,设置子控件属性

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 文本居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 改变图片的内容模式
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return self;
}
  • 也可重写layoutSubViews设置子控件的frame
// 方法二
- (void)layoutSubviews{
    // 注意:这里一定要调用父类的方法!!!!!
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(0, 0, 100, 50);
    self.imageView.frame = CGRectMake(100, 0, 50, 50);
}
  • 设置按钮的边距
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置按钮的内边距
    //1.设置内容
//    self.button.contentEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
    
    // 2.设置图片
     self.button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    // 3.设置标题
    self.button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
}

你可能感兴趣的:(#UIButton#自定义UIButton)