前面写了UIWindow、UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实现是使用系统提供的各种控件,其他的就是自定义实现了,作者目前是入门状态,只能写写基础控件了。
iOS中提供了UIButton、UILable、UITextField、UIImageView等基础UI控件,继承于UIView。这里先拿UIButton练练手,为什么拿UIButton呢,因为UIbutton继承自UIControl,UIControl派生自UIView类,每个控件都有很多视图的特性,包括附着于其他视图的能力,所有控件都拥有一套共同的属性和方法,包含显示内容,点击事件等等,UIControl的子类都有事件处理能力。
图、UIControl
UIButton的定义:
UIButton可使用initWithFrame、buttonWithType两种方式创建:
1)initWithFrame
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(60, 60, 200, 60)];
btn.backgroundColor=[UIColor greenColor];
[btn setTitle:@"btn1" forState:UIControlStateNormal];
[self.view addSubview:btn];
2)buttonWithType
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect=CGRectMake(60, 160, 200, 60);
btn2.frame=rect;
btn2.tag=1001;
btn2.backgroundColor=[UIColor colorWithRed:30/255.0 green:200/255.0 blue:125/255.0 alpha:1.0];
[btn2 setTitle:@"btn2" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(btn2Pressed) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn2];
btn2Pressed方法:
-(void)btn2Pressed{
NSLog(@"button pressed");
}
UIButtonType:
typedef enum {
UIButtonTypeCustom = 0, // no button type 自定义,无风格
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;
UIButton常用属性:
//设置对应状态的标题内容default is nil. title is assumed to be single line
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置对应状态的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的标题阴影颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//设置对应状态的按钮的图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置对应状态的按钮背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
UIButton的UIControlState :
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
更多属性可参考官方文档。
UIButton添加事件:
UIButton使用如下方法添加事件。
[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>]
这些事件都是基于触摸、基于值、基于编辑。可相应如下事件。
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // on all touch downs
UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12, // sliders, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF
};
* iOS入门群:83702688
* android开发进阶群:241395671
*/
参考:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW1
一、UIKit结构图
分享到:
相关推荐
代码中是JSPatch学习笔记的UI基础部分,其中包括以下内容: 1. 项目环境配置 2. 变量 * 局部变量 * 全局变量 * 使用OC中的枚举类型 `Objective-C 里的常量不能直接在 JS 上使用,可以直接在 JS 上用具体值代替,...
* 自定义图片上下左右位置 * 自定义文字和图片间距 * 自定义角标动画 * 支持CocoaPods github地址: https://github.com/JunAILiang/JMButton 简书地址: https://www.jianshu.com/p/ba8e986cdd0c ...
iOS学习笔记 十年生死两茫茫,不思量,自难忘,华年短暂,陈辞岁月悠悠伤, 满腔热血已芜荒,展未来,后生强,战战兢兢,如履薄冰心彷徨, 青丝化雪、鬓角成霜,已是英雄迟暮,人生怎慷慨激昂? 目录 内存管理(五) ...
ios 自定义uibutton
使用一句代码为UIbutton增加不同状态的配置(borderColor, titleLabelFont, backgroundColor,subView) 最新代码请移步github: https://github.com/spWang/UIButton-State 如果在使用过程中发现问题,请联系我,...
主要介绍了IOS 通过tag删除动态创建的UIButton的相关资料,需要的朋友可以参考下
详情描述请来这里: http://www.chinabooks99.top/index.php/2016/07/14/swfit3-0_uilableuibuttonuitableview/
借鉴相关资料,整理了一个很有意思的button动画效果,iOS自定义UIButton点击动画特效 先看一下效果图: 下面贴上代码: ViewController: #import @interface ViewController : UIViewController @end #import ...
一句代码设置UIButton的image和title的左右上下布局以及image和title的间距,git地址:https://github.com/Coder-ZJ/ZJCustomButton
Swift编程-UI控件-UIButton(一)视频教程
iphone中的基础控件UIButton, 适合初学者,主要学习UIButton的常用属性设置、方法调用、点击事件等
相信每位iOS开发者都知道在UIButton中可以设置图片和文字,也经常见到同时设置有图片和下方提示文字的按钮,但是当我自己去对图片按钮添加提示文字的时候,却发现这并不是想象中的那么简单。怎么不简单呢?下面来...
DFCountDownButton,继承UIButton,实现IOS倒计时按钮,用于注册等发送验证码的时候进行倒计时操作
可以自定义image和title布局,修改高亮状态图片,背景色,渲染图片颜色,高度自定义 详细文档:http://www.jianshu.com/p/4603e9bbba56 github:... 最新版本将会上传到github
IOS 不规则按钮实现方法 直接引用即可
xamarin学习笔记B03(ios的UIView的了解)它是所有视图控件的父类。例如UIButton、UILabel、UIImageView等都最终是继承自UIView。它可以添加其它视图作为它的子视图。
包含collectionView,UIBezierPath,ActionSheet,DatePicker,pickerView,SearchBar,UIActivityIndicatorView,UIAlertView,UIButton,UIimagePicker,UILabel,UInavigationController,UIProgressView,UIScrollerView,...