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 ...
随机推荐
- flat network 原理与配置 - 每天5分钟玩转 OpenStack(86)
flat network 是不带 tag 的网络,要求宿主机的物理网卡直接与 linux bridge 连接,这意味着: 每个 flat network 都会独占一个物理网卡. 上图中 eth1 桥接 ...
- Don't let self-built concept imprison yourself
If Self-inferiority is disease, but self-confidence is hazard. Leo moon personalities can be extreme ...
- Android动画学习(二)——Tween Animation
前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...
- 3d效果的图片轮播
CSS3的3d变换 CSS3给我们提供了一个新的功能,那就是3d变换.3d变换和2d变换的基本API函数类似,只不过多了些在Z轴上的操作,不难使用. 但是,为了让元素拥有3d变换的功能,我们需要给他的 ...
- Javascript的无new构建
看jquery源代码第一步的时候,对于jquery对象的创建就看的云里雾里,琢磨半天终于有点感觉了,在此记录下 第一种方式: var A = function(){ return A.prototyp ...
- java多线程--多线程基础小结
什么是线程? 在同一个进程中可以执行多个任务,每一个任务可以看做一个线程. 线程是程序的执行单元,执行路径,使程序使用cpu的最基本单位 一个进程如果只有一条执行路径,那么就是单线程的 一个进程如果有 ...
- 重温JSP学习笔记--El函数库
EL函数库(由JSTL提供的) * 导入标签库:<%@ tablib prefix="fn" uri="http://java.sun.com/jsp/jstl/f ...
- 关于一道PHP面试题的解法
参照一个int型数组,如int[] a1=new int[]{10,9,10,20,15,3,9,8,7,1,1},编写一个方法,要求输出不重复,且降序的拼接字符串(连接字符用逗号),如上数组,输出的 ...
- jQuery的$.getJSON方法在IE浏览器下失效的解决方案
$.getJSON在IE下默认会使用浏览器缓存,所以导致数据不正确或者异常,解决方案就是在使用该方法前关闭缓存,使用完后再重新打开缓存即可. <?php $.ajaxSetup({ cache: ...
- OpenCV2邻域和模板操作
在图像处理中,通过当前位置的邻域像素计算新的像素值是很常见的操作.当邻域包含图像的上几行和下几行时,就需要同时扫描图像的若干行,这就是图像的邻域操作了.至于模板操作是实现空间滤波的基础,通常是使用一个 ...