iOS - UIButton的重難點(diǎn)使用方法

這篇文章不介紹按鈕的基本用法,主要介紹按鈕的難點(diǎn),項(xiàng)目中常會(huì)碰到的問(wèn)題,現(xiàn)在總結(jié)出來(lái)。

一 . 關(guān)于UIButton的titleimage位置問(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];
運(yùn)行結(jié)果

這樣的結(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];

調(diào)節(jié)后的

根據(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];

QQ20170218-115704@2x.png
    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];

QQ20170218-120012@2x.png

三、關(guān)于UIButton調(diào)整內(nèi)容的屬性介紹。

重點(diǎn)來(lái)了,通過(guò)設(shè)置UIButtoncontentEdgeInsets屬性和UIControlUIControlUIButton的父類)的contentVerticalAlignmentcontentHorizontalAlignment屬性可以將按鈕中的內(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];


QQ20170218-122554@2x.png

四、關(guān)于UIButton文字字體大小。

button.titleLabel.font = [UIFont systemFontOfSize:50];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 版本 Xcode 9.1 繼承關(guān)系:UIButton : UIControl : UIView : UIRespo...
    看影成癡閱讀 5,119評(píng)論 0 5
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,734評(píng)論 1 14
  • 對(duì)象繼承關(guān)系 UIButton 類本身定義繼承 UIControl ,描述了在 iOS 上所有用戶界面控件的常見(jiàn)基...
    獨(dú)木舟的木閱讀 3,943評(píng)論 0 3
  • 文/正霖 一潭靜水映晚霞。 策馬揮鞭,仗劍天涯。 前路漫漫誰(shuí)人伴? 霜冷長(zhǎng)夜,月色無(wú)瑕。 懸崖盡頭彼岸花。 何必懼...
    正霖居士閱讀 311評(píng)論 10 6
  • Be_Jessica閱讀 240評(píng)論 0 0

友情鏈接更多精彩內(nèi)容