iOS --按鈕 處理

weixin_34120274發表於2015-11-12

一、目錄

  • 1.如何實現圓角 處理方案
  • 2.上圖下文按鈕:自定義按鈕

二、如何實現圓角

831339-4435d7977304db06.png
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 設定
831339-a2c5d00e26015dd8.png
Snip20150902_85.png
2、自定義按鈕
831339-09ebdb56d759223b.png
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兩個子控制元件的內邊距,來調整它們自己的佈局

相關文章