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

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

最新文章

  1. winform快速开发平台 -&gt; 快速绑定ComboBox数据控件
  2. 在cmd命令行使用Maven Archetype插件 generate命令创建简单的java web项目
  3. Join的表顺序
  4. 启动Hadoop HA Hbase zookeeper spark
  5. js原生捕鱼达人(二)
  6. AJAX练习(一):制作可以自动校验的表单(从原理上分析ajax的作用)
  7. android.mk文件里的通配符
  8. 徐汉彬:亿级Web系统搭建——单机到分布式集群(转载)
  9. Codevs 1218 疫情控制 2012年NOIP全国联赛提高组
  10. MongoDB库设计原则及实践
  11. 使用Project进行挣值分析
  12. JavaWeb从0开始学(一)-----搭建第一个Web应用程序与JSP工作原理
  13. 博弈论进阶之SG函数
  14. 改xml
  15. 012_如何清除DNS缓存
  16. HDU2019数列有序!
  17. Anagram 由颠倒字母顺序而构成的字
  18. NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
  19. 剑指offer三十八之二叉树的深度
  20. 【BZOJ】【3503】【CQOI2014】和谐矩阵

热门文章

  1. 绿色地狱 - 纽博格林赛道详解 | N&#252;rburgring
  2. golang rabbitmq 的学习
  3. C# 实现HTTP的POST(完整可运行并且通过测试的代码)
  4. Python 初级 6 循环
  5. 日志分类以及TFIDF
  6. 【翻译】Flink Table Api &amp; SQL — 用户定义函数
  7. 【VS开发】MFC学习之 解决StretchBlt()图片缩放绘图失真
  8. Jmeter3.1 使用及新增报告功能
  9. OpenGL和EGL入门
  10. 如何使用websocket实现前后端通信