为了账号安全,请及时绑定邮箱和手机立即绑定

iOS UIButton之UIButtonType详解

标签:
iOS

我做了一个关于UIButtonType的Demo,效果如下图:


webp

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



作者:QiShare
链接:https://www.jianshu.com/p/35204f62199e


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消