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 ...
随机推荐
- 【开源】OSharp3.3框架解说系列:开发计划与进度
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- Util应用程序框架公共操作类(一):数据类型转换公共操作类(介绍篇)
本系列文章将介绍一些对初学者有帮助的辅助类,这些辅助类本身并没有什么稀奇之处,如何能发现需要封装它们可能更加重要,所谓授之以鱼不如授之以渔,掌握封装公共操作类的技巧才是关键,我会详细说明创建这些类的动 ...
- ES6 - Note4:Class类
1.Class类的介绍 在ES6中新增了Class类的概念,让语法看起来更像是面向对象编程,其实这可以说是一个语法糖,ES5可以做到Class绝大部分功能,但也有一些不同.在ES6以前,可以通过构造函 ...
- 如何用Perl截取报文
在实际生产环境中,常常需要从后台日志中截取报文,报文的形式类似于 <InterBOSS> ... ... ... </InterBOSS> 一个后台日志有多个报文,每个报文可由 ...
- OWIN 中 K Commands 与 OwinHost.exe 相等吗?
OwinHost.exe: While some will want to write a custom process to run Katana Web applications, many wo ...
- Last-Modified、If-Modified-Since 实现缓存和 OutputCache 的区别
先梳理三个概念: OutputCache:页面输出缓存,一般 ASP.NET 应用程序会用到. Last-Modified:Http 响应头(Http Reponse Header),由服务器发给客户 ...
- js 对Array的补充
Array.form的用法 1.可以将各种值转为真正的数组,并且还提供map功能.这实际上意味着,只要有一个原始的数据结构,你就可以先对它的值进行处理,然后转成规范的数组结构,进而就可以使用数量众多的 ...
- iOS chart 图表完美解决方案 基于swift
如果打算在app中使用图标功能,这个框架基本能够满足90%的需求 下边是作者的框架的下载地址 ,基于swift2.0 https://github.com/danielgindi/ios-charts ...
- 虚拟机利用Host-only实现在不插网线的情况下,虚拟机与主机实现双向通信,实现ssh连接以及samba服务实现共享
为了不影响其他的虚拟网卡,我们在VMware下在添加一块虚拟网卡: 然后点击Next,选择连接方式: 点击Finish即可. 重新启动虚拟机,如果这是你手动添加的第一块虚拟网卡,那么应该是eth1. ...
- 跨域post请求实现方案小结--转
[名词解释] 跨域:https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript 同源策略 ...