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 ...
随机推荐
- JBOSS.71.1.Final安装配置
对于JBOSS大家了解多少,相信做Java开发的小童鞋对于Tomcat一定不陌生,而今天为大家介绍的JBOSS也是一款服务器软件,相比Tomcat,JBOSS对于高级的JavaEE相对来说更强大一点, ...
- Android 之 ProgressDialog用法介绍
布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- The network bridge on device VMnet0 is not running
The network bridge on device VMnet0 is not running. The virtual machine will not be able to communic ...
- linux下遍历目录
遍历目录的主要思想 由于目录就是一颗树,所以遍历目录就转换为遍历一棵树.谈到树的遍历就再熟悉不过了,有树的前序.层次和后序遍历,我使用的是前序遍历,后序遍历和前序遍历本质上一样,而层次遍历要比前两个麻 ...
- 读《linux内核完全注释》的FAQ
以下只是个人看了<linux内核完全注释>的一点理解,如果有错误,欢迎指正! 1 eip中保存的地址是逻辑地址.线性地址还是物理地址? 这个应该要分情况.eip保存的是下一条要执行的指令地 ...
- [CUDA] CUDA to DL
又是一枚祖国的骚年,阅览做做笔记:http://www.cnblogs.com/neopenx/p/4643705.html 这里只是一些基础知识.帮助理解DL tool的实现. “这也是深度学习带来 ...
- Windows Phone 的 TextBox 的实现 PropertyChanged
比如,View 的文本框 TextBox1 绑定了 ViewModel 的 Msg 属性, 当想把文本框输入的内容输入过程中实时更新到绑定的 Msg ,在Windows Phone 中是无法通过设置 ...
- Web 分页
以Car表为例分页 页面以table展示数据分页 页面代码 <asp:Repeater ID="Repeater1" runat="server"> ...
- 【Win10开发】Toast通知——后台激活
前篇文章我们写了关于Toast的前台激活,那么接下来就讲一讲它的后台激活.当通知出现时并不会出现app的界面,但是app能在后台获取到通知中的信息. 关于xaml和Toast通知架构我们在这里就不再赘 ...
- linq lambda let
1.linq let用法 var query = from x in db.Users let theage = x.age let sexstring = x.sex ? "男" ...