iOS UIButton之UIButtonType詳解

QiShare發表於2018-08-09

級別: ★☆☆☆☆
標籤:「UIButton的使用場景」「UIButtonType」
作者: WYW

我做了一個關於UIButtonType的Demo,效果如下圖:

UIButtonType.gif

UIButtonType各個型別的解釋:

typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0,                         
    UIButtonTypeSystem,  
    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,
    UIButtonTypeContactAdd,
    UIButtonTypePlain, 
    UIButtonTypeRoundedRect = UIButtonTypeSystem
};
複製程式碼
  • UIButtonTypeCustom:
    官方:No button style.
    解釋:自定義的按鈕(無樣式)

  • UIButtonTypeSystem:
    官方:A system style button, such as those shown in navigation bars and toolbars.
    解釋:系統樣式

  • UIButtonTypeDetailDisclosure:
    官方: A detail disclosure button.
    解釋:細節詳情樣式

  • UIButtonTypeInfoLight:
    官方:An information button that has a light background.
    解釋:按鈕圖片為i字母(info)亮的資訊型別

  • UIButtonTypeInfoDark:
    官方:An information button that has a dark background.
    解釋:按鈕圖片為i字母(info)暗的資訊型別

注意: iOS7及之後,只有在設定showsTouchWhenHighlighted為YES的時候,DetailDisclosure的外觀和InfoLight/InfoDark不同(測試的時候我並沒有看出來什麼不同,如果你看出來了,勞煩告訴我),其他情況下都相同

  • UIButtonTypeContactAdd:
    官方:A contact add button.
    解釋:加號(➕)按鈕型別

  • UIButtonTypePlain:
    官方:A standard system button without a blurred background view.
    解釋:沒有模糊背景檢視的標準的系統按鈕 不過只支援 tvOS

  • UIButtonTypeRoundedRect = UIButtonTypeSystem:
    官方:A rounded-rectangle style button.
    解釋:方形的圓角形式的按鈕,在iOS7後被廢棄,現在需要使用border的方式來做到效果
    注意:(UIButtonTypeRoundedRect已廢棄, UIButtonTypeRoundedRect的列舉值為1 !

相關程式碼


#import "QiButton_ButtonTypeViewController.h"


@interface QiButton_ButtonTypeViewController ()

@end

@implementation QiButton_ButtonTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"UIButtonType";
    
    [self buttonType];
}



#pragma mark - Private functions

- (void)buttonType {
    
    NSArray <NSString *>*buttonTypes = @[@"UIButtonTypeCustom",
                                         @"UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0)",
                                         @"UIButtonTypeDetailDisclosure",
                                         @"UIButtonTypeInfoLight",
                                         @"UIButtonTypeInfoDark",
                                         @"UIButtonTypeContactAdd",
                                         @"UIButtonTypePlain API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios, watchos)",
                                         @"7 = UIButtonTypePlain | UIButtonTypeSystem",
                                         @"UIButtonTypeRoundedRect = UIButtonTypeSystem",
                                         @"new a Button"];
    CGFloat btnHeight = [UIScreen mainScreen].bounds.size.height / buttonTypes.count;
    
    for (NSInteger buttonTypeI = 0 ; buttonTypeI < buttonTypes.count; buttonTypeI ++) {
        UIButton *buttonTypeBtn = [UIButton buttonWithType:buttonTypeI];
        // 設定最後的一個按鈕 new的方式建立
        if (buttonTypeI == buttonTypes.count - 1) {
            // 經測試 列印的btn.buttonType 為 UIButtonTypeCustom 觀察button的顯示樣式也是如此
            buttonTypeBtn = [UIButton new];
            [buttonTypeBtn setImage:[UIImage imageNamed:@"smallQiShareLogo"] forState:UIControlStateNormal];
            [buttonTypeBtn setImage:[UIImage imageNamed:@"smallQiShareLogo"] forState:UIControlStateHighlighted];
        } else if(buttonTypeI == buttonTypes.count - 2) {
            /** 注意UIButtonTypeRoundedRect = UIButtonTypeSystem 真正的值為 1 而不是7
                如果以 [UIButton buttonWithType:7] 方式建立UIButton
                相當於 [UIButton buttonWithType:UIButtonTypePlain | UIButtonTypeSystem];
             */
            buttonTypeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [buttonTypeBtn setImage:[UIImage imageNamed:@"smallQiShareLogo"] forState:UIControlStateNormal];
            [buttonTypeBtn setImage:[UIImage imageNamed:@"smallQiShareLogo"] forState:UIControlStateHighlighted];
        } else if(buttonTypeI == UIButtonTypeCustom || buttonTypeI == UIButtonTypeSystem || buttonTypeI == UIButtonTypeRoundedRect) {
            [buttonTypeBtn setImage:[UIImage imageNamed:@"smallQiShareLogo"] forState:UIControlStateNormal];
            [buttonTypeBtn setImage:[UIImage imageNamed:@"smallQiShareLogo"] forState:UIControlStateHighlighted];
        } else if(buttonTypeI == UIButtonTypeDetailDisclosure || buttonTypeI == UIButtonTypeInfoLight || buttonTypeI == UIButtonTypeInfoDark) {
            buttonTypeBtn.showsTouchWhenHighlighted = YES;
        }
        [self.view addSubview:buttonTypeBtn];
        buttonTypeBtn.frame = CGRectMake(.0, buttonTypeI * btnHeight, CGRectGetWidth(self.view.frame), btnHeight);
        buttonTypeBtn.backgroundColor = (buttonTypeI % 2 ? [UIColor lightGrayColor] : [UIColor colorWithWhite:0.8 alpha:0.8]);
        [buttonTypeBtn setTitle:buttonTypes[buttonTypeI] forState:UIControlStateNormal];
        buttonTypeBtn.titleLabel.numberOfLines = 0;
        
        [buttonTypeBtn addTarget:self action:@selector(buttonTypeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
}


#pragma mark - Action functions

- (void)buttonTypeButtonClicked:(UIButton *)sender {
    
    sender.selected = !sender.selected;
}

@end

複製程式碼

程式碼GitHub地址

參考學習網址

關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

相關文章