UIButton
//UIButton->UIControl->UIView
//UIControl 带有操作的控件都是继承于它的
//UIButton 实例化 类方法实例化
//实例化时没有位置及大小,需设置frame属性
/*
1、UIButtonTypeSystem = UIButtonTypeRoundedRect iOS7之前UIButtonTypeRoundedRect带有圆角效果,iOS7之后才没有的
2、UIButtonTypeInfoLight = UIButtonTypeInfoDark = UIButtonTypeDetailDisclosure 蓝色的圆圈i
3、UIButtonTypeContactAdd 蓝色的圆圈+
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//frame
button.frame = CGRectMake(20, 100, 280, 40);
//属性
button.backgroundColor = [UIColor redColor];
//按钮是否可用: NO:不可用 YES:可用,默认状态
button.enabled = YES;
//设置文字:setTitle:forState:
/*
1、UIControlStateNormal 一般状态
2、UIControlStateHighlighted 高亮状态
3、UIControlStateDisabled 禁用状态
*/
[button setTitle:@"button" forState:UIControlStateNormal];
// [button setTitle:@"highlighted" forState:UIControlStateHighlighted];
[button setTitle:@"disabled" forState:UIControlStateDisabled];
//设置文字颜色:默认蓝色 setTitleColor:forState:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//设置文字大小:.titleLabel.font
button.titleLabel.font = [UIFont systemFontOfSize:17.0];
//文字对齐方式
//水平方向:contentHorizontalAlignment
/*
1、UIControlContentHorizontalAlignmentCenter 居中
2、UIControlContentHorizontalAlignmentLeft 左对齐
3、UIControlContentHorizontalAlignmentRight 右对齐
*/
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//垂直方向:contentVerticalAlignment
/*
1、UIControlContentVerticalAlignmentBottom 下对齐
2、UIControlContentVerticalAlignmentCenter 居中
3、UIControlContentVerticalAlignmentTop 上对齐
*/
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//**按钮的点击事件**//
/*
1、第一个参数:按钮点击之后通知到谁:self
2、第二个参数:按钮的事件:@selector(方法名字)
3、第三个参数:按钮的点击方式
*/
//按钮事件不带参数的样式
// [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
//按钮事件带参数的样式
[button addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchUpInside];
/*
按钮的点击方式
1、UIControlEventTouchUpInside 里进里出
2、UIControlEventTouchUpOutside 里进外出
3、UIControlEventTouchDragInside 里进里拖拽
4、UIControlEventTouchDragOutside 里进外拖拽
5、UIControlEventTouchDragExit 里进拖拽出去
6、UIControlEventTouchDragEnter 里进拖拽出去再拽回去
7、UIControlEventTouchDown 单击
8、UIControlEventTouchDownRepeat 双击
*/
//添加到父视图上
[self.window addSubview:button];
#pragma mark - 按钮的点击事件
//带参数的方法实现
- (void)buttonDown:(UIButton *)button{
NSLog(@"ddd");
self.window.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
//获取按钮的文字
//currentTitle:如果按钮有高亮状态,直接获取到高亮状态下的文字,如果只存在nomal状态,获取nomal状态下的文字
NSString *title = button.currentTitle;
//.titleLabel.text:如果按钮的高亮状态在点击时没出来,获取nomal状态下的文字,如果高亮状态出来,获取高亮状态下的文字;如果按钮只存在nomal状态,获取nomal状态下的文字
NSString *title2 = button.titleLabel.text;
NSLog(@"currentTitle=%@;titleLabel=%@",title,title2);
}
//不带参数的方法实现
- (void)buttonClick{
// arc4random() 随机数 arc4random()%256 随机:0-255
// arc4random_uniform(256)
self.window.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
UIButton的更多相关文章
- AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking
AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...
- 记录下UIButton的图文妙用和子控件的优先显示
UIButton的用处特别多,这里只记录下把按钮应用在图文显示的场景,和需要把图片作为按钮的背景图片显示场景: 另外记录下在父控件的子控件优先显示方法(控件置于最前面和置于最后面). 先上效果图: 1 ...
- iOS学习-UIButton的imageView和titleLabel
UIButton的imageView和titleLabel的位置设置通过setImageEdgeInsets和setTitleEdgeInsets来设置 参考:http://blog.csdn.net ...
- iOS小知识:使UIButton中的图片和文字实现左对齐
UIButton setImage 和 setTitle之后,默认的 image和title 对齐方式是居中, 由于 title 长度不固定,所以如果要几个这样有image有title的按钮纵向排列对 ...
- UIButton无法响应点击事件
一.问题描述 因为项目需要,需要UITableView上添加固定的筛选表头,一直固定,不能随UITableView滚动.所以直接将表头与UITableView分离,将它添加到控制器的UIView上,即 ...
- 布局包含Image和Title的UIButton
UIButton中的titleEdgeInsets和imageEdgeInsets可以管理button中image和title的布局. 如果对其理解不够深入,用纯数字进行布局管理,经过不断的调试,还是 ...
- UI控件(UIButton)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // UIButtonTypeCustom = 0, ...
- 自定义UIButton
偶尔逛简书能看见很多值得记下来的东西,有的接触过有的没接触过,接触过的也可能过段时间就忘记了,再上手的时候可能手足无措,所以决定有些觉得值得记下来的东西还是记录一下.博客是个好地方,因为很多人都能搜索 ...
- UITableView或UIScrollVIew上的UIButton的高亮效果
UITableView或UIScrollVIew上的UIButton的高亮效果 原文地址:http://www.jianshu.com/p/b4331f06bd34 最近做项目的时候发现,UIScro ...
随机推荐
- WinForm最小化到托盘以及托盘右键菜单
首先,先拖一个NotifyIcon到主窗体,然后设置NotifyIcon的图标,不然等下最小化后,都找不到那个程序了,还有那个Text也是,不写名字,就默认是NotifyIcon了..如下图: 然后双 ...
- 窥探Swift系列博客说明及其Swift版本间更新
Swift到目前为止仍在更新,每次更新都会推陈出新,一些Swift旧版本中的东西在新Swift中并不适用,而且新版本的Swift会添加新的功能.到目前为止,Swift为2.1版本.去年翻译的Swift ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
- Hammer.js分析(二)——manager.js
“Manager”是所有识别器实例的容器,它为你设置的元素安装了交互事件监听器,并设置了触摸事件特性. manager.js中的代码会涉及到input.js和recoginzer.js中的内容,这里会 ...
- MVC前台Post/Get异步获得数据时参数的取值问题
Post方法,返回text,后台获得Data View $.ajax({ type: "POST", dataType: "text",//返回类型为文本 ur ...
- JS实现返回对象的详细信息
使用JS有时会需要打印出对象的详细信息,下面方法可以实现: function ShowObjProperty(Obj) { var PropertyList=''; var PropertyCount ...
- tomcat源码剖析系列
一个简单的web服务器 一个简单的servlet容器 连接器 创建httpRequest 创建HttpResponse 容器 生命周期 日志记录器 载入器 Session管理 关闭钩子 启动tomca ...
- 以太坊智能合约Hello World示例程序
简介 以太坊(Ethereum)是一提供个智能合约(smart contract)功能的公共区块链(BlockChain)平台. 本文介绍了一个简单的以太坊智能合约的开发过程. 开发环境 在以太坊上开 ...
- 利用闭包解决for循环里onclick事件不能捕捉实时i值问题
问题描述 我们都知道,如果我们对于一组元素(相同的标签)同时进行onclick事件处理的时候(在需要获取到索引的时候),一般是写一个for循环,但是onclick是一个异步调用的,所以会带来一个问题, ...
- entity framework 删除数据库出现错误的解决方法--最土但是很有效的方法
无法删除数据库,因为该数据库当前正在使用. public ChinaerContext() : base("name=ContextConn") { // Database.Set ...