iOS --按钮 处理

一、目录

  • 1.如何实现圆角 处理方案
  • 2.上图下文按钮:自定义按钮

二、如何实现圆角

iOS --按钮 处理_第1张图片
Snip20150902_75.png
1. 方案一:设置按钮的layer属性
  self.loginButton.layer.cornerRadius = 5;
  self.loginButton.layer.masksToBounds = YES; // 裁剪
2. 方案二:KVC 修改 layer属性
  [self.loginButton setValue:@5 forKeyPath:@"layer.cornerRadius"];
  [self.loginButton setValue:@YES forKeyPath:@"layer.masksToBounds"];
3. 方案三:通过storyboard / xib 设置
iOS --按钮 处理_第2张图片
Snip20150902_85.png
2、自定义按钮
iOS --按钮 处理_第3张图片
Snip20151027_3.png
  • 方案一:在自定义按钮的layoutSubviews方法中,调整按钮子控件imageView/titleLabel的位置
  - (void)awakeFromNib
{
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    // 调整图片的位置和尺寸
    self.imageView.y = 0;
    self.imageView.centerX = self.width * 0.5;

    // 调整文字的位置和尺寸
    self.titleLabel.x = 0;
    self.titleLabel.y = self.imageView.height;
    self.titleLabel.width = self.width;
    self.titleLabel.height = self.height - self.titleLabel.y;
}
  • 方案二:重写自定义按钮的两个方法,imageRect.... / titleRect......方法调整两个子控件的frame布局子控件

  • 方案三:当然我们也可以在initWithFrame或者awakeFromNib来调整imageView/titleLabel两个子控件的内边距,来调整它们自己的布局

你可能感兴趣的:(iOS --按钮 处理)