這篇文章不介紹按鈕的基本用法,主要介紹按鈕的難點(diǎn),項(xiàng)目中常會(huì)碰到的問(wèn)題,現(xiàn)在總結(jié)出來(lái)。
一 . 關(guān)于UIButton的title和image位置問(wèn)題。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(90, 180, 100, 40);
btn.backgroundColor = [UIColor yellowColor];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitle:@"title" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"button_help_small"] forState:UIControlStateNormal];
[self.view addSubview:btn];

這樣的結(jié)果不太好看吧。因?yàn)閳D片和文字都緊緊的挨在一起了。所以咱們可以調(diào)節(jié)兩者的距離,下面這兩個(gè)屬性就是調(diào)節(jié)距離的。
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
@property(nonatomic) UIEdgeInsets imageEdgeInsets;
然后看我寫(xiě)的代碼:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(90, 180, 100, 40);
btn.backgroundColor = [UIColor yellowColor];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitle:@"title" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"button_help_small"] forState:UIControlStateNormal];
// UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
// 上左下右
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 10, 0);
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, -10, 0);
[self.view addSubview:btn];

根據(jù)這個(gè)例子,舉一反三,你是不是可以讓按鈕上的圖片和文字各種位置擺放,想放哪里放哪里。
二 . 關(guān)于UIButton只有圖片的問(wèn)題。
當(dāng)UIButton在設(shè)置背景圖片的時(shí)候,我們會(huì)遇到這樣的問(wèn)題,有時(shí)候設(shè)計(jì)的UI尺寸比較小,那樣會(huì)導(dǎo)致可點(diǎn)擊區(qū)域太小,用戶體驗(yàn)不好,但設(shè)計(jì)的UI又不能更改,影響美觀,所以這里就涉及到設(shè)置UIButton背景圖片的兩個(gè)方法,image和BackgroundImage。image是圖片不會(huì)被拉伸。BackgroundImage會(huì)拉伸。這在項(xiàng)目中看需求用。
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(90, 180, 100, 40);
btn.backgroundColor = [UIColor yellowColor];
// 方法一:該方法不管UIButton設(shè)置的尺寸為多大,只居中顯示圖片的原始尺寸
// 該方法可以在不改變?cè)荚O(shè)計(jì)UI的尺寸前提下,擴(kuò)大可點(diǎn)擊區(qū)域
[btn setImage:[UIImage imageNamed:@"button_help_small"] forState:UIControlStateNormal];
[self.view addSubview:btn];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(90, 180, 100, 40);
btn.backgroundColor = [UIColor yellowColor];
// 方法二:該方法會(huì)把圖片伸縮至UIButton設(shè)置的尺寸大小
// 如果想讓圖片不變形,就只能就UIButton的尺寸設(shè)置成image的大小
[btn setBackgroundImage:[UIImage imageNamed:@"button_help_small"] forState:UIControlStateNormal];
[self.view addSubview:btn];

三、關(guān)于UIButton調(diào)整內(nèi)容的屬性介紹。
重點(diǎn)來(lái)了,通過(guò)設(shè)置UIButton的contentEdgeInsets屬性和UIControl(UIControl是UIButton的父類)的contentVerticalAlignment和contentHorizontalAlignment屬性可以將按鈕中的內(nèi)容(圖片或者文字或者圖片和文字的整體)整體移動(dòng)。
UIButton *button = [[UIButton alloc] init];
//設(shè)置坐標(biāo)
button.frame = CGRectMake(100, 200, 200, 50);
//設(shè)置標(biāo)題
[button setTitle:@"呵呵" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"button_help_small"] forState:UIControlStateNormal];
//設(shè)置標(biāo)題顏色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//設(shè)置背景顏色
[button setBackgroundColor:[UIColor orangeColor]];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[self.view addSubview:button];

四、關(guān)于UIButton文字字體大小。
button.titleLabel.font = [UIFont systemFontOfSize:50];