代碼創建
//創建UIButton
UIButton * btnType=[[UIButton alloc]init];
//設置UIControlStateNormal狀態下的文字顏色
[btnType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//設置字體大小
btnType.titleLabel.font=[UIFont systemFontOfSize:9.0];
//設置邊框的寬度
btnType.layer.borderWidth=1;
//設置邊框的顏色
btnType.layer.borderColor=[[UIColor lightGrayColor]CGColor];
//設置UIControlStateNormal的文字
[btnType setTitle:@"按鈕設置名字" forState:UIControlStateNormal];
//設置UIControlStateNormal的圖片
[btnType setImage:[UIImage imageNamed:@"獲取Assets.xcassets圖片名稱"] forState:UIControlStateNormal];
//設置UIControlStateNormal背景圖片
[btnType setBackgroundImage:[UIImage imageNamed:@"獲取Assets.xcassets圖片名稱"] forState:UIControlStateNormal];
//設置失效狀態
btnType.enabled=NO;
監聽按鈕點擊
//只要按鈕觸發了UIControlEventTouchUpInside事件,就調用self對象buttonClick方法
[btn addTarget:self action:@selector(buttonClick) forCOntrolEvents:UIControlEventTouchUpInside];
自定義按鈕
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
// 返回文字的frame
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
// 返回圖片的frame
}
按鈕顯示的狀態
設置不能點擊 enabled 等於NO
adjustsImageWhenDisabled 等於NO 在Disabled下要不要調整顯示的圖片
態
adjustsImageWhenHighlighted 高亮下不要調整圖片
按鈕內邊距
通過代碼設置 contentEdgeINsets = uiedgeInsetsMake 設置內邊距
contentEdgeInsets=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);
設置文字 titleEdgeInsets=uiedgeInsetsMake 設置文字的內邊距
titleEdgeInsets=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);
設置圖片 imageEdgeInsets=uiedgeInsetsMake 設置圖片的內邊距
imageEdgeInsetss=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);
微小調整使用內邊距 復雜的話使用自定義按鈕