OS UIButton之UIButtonType详解-转
我做了一个关于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.
解释:没有模糊背景视图的标准的系统按钮 不过只支持 tvOSUIButtonTypeRoundedRect = 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
最新文章
- winform快速开发平台 ->; 快速绑定ComboBox数据控件
- 在cmd命令行使用Maven Archetype插件 generate命令创建简单的java web项目
- Join的表顺序
- 启动Hadoop HA Hbase zookeeper spark
- js原生捕鱼达人(二)
- AJAX练习(一):制作可以自动校验的表单(从原理上分析ajax的作用)
- android.mk文件里的通配符
- 徐汉彬:亿级Web系统搭建——单机到分布式集群(转载)
- Codevs 1218 疫情控制 2012年NOIP全国联赛提高组
- MongoDB库设计原则及实践
- 使用Project进行挣值分析
- JavaWeb从0开始学(一)-----搭建第一个Web应用程序与JSP工作原理
- 博弈论进阶之SG函数
- 改xml
- 012_如何清除DNS缓存
- HDU2019数列有序!
- Anagram 由颠倒字母顺序而构成的字
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
- 剑指offer三十八之二叉树的深度
- 【BZOJ】【3503】【CQOI2014】和谐矩阵