*初始化UIButton控件显示在屏幕上
1、常规的initWithFrame
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(100, 100, 80, 60)];
[button setBackgroundColor: [UIColor blueColor]];
[button setTitle: @"点击" forState: UIControlStateNormal];
[self.view addSubview: button];
2、UIButton的一个类方法buttonWithType
UIButton *button = [UIButton buttonWIthType: UIButtonTypeRoundedRect];
[button setFrame: CGRectMake(100, 100, 80, 60)];
[button setBackgroundColor: [UIColor blueColor]];
[button setTitle: @"点击" forState: UIControlStateNormal];
[self.view addSubview: button];
基本属性
1、设置属性
(1)Frame: 坐标; title; titlecolor: 字体颜色; titleShadowColor: 字体阴影; image: 图片; backgroudImage: 背景图片;
(2)forstate 状态: 这个参数决定了标题、图像或其他属性将在何种状态下显现。
Enum{
UIControlStateNormal; 常态
UIControlStateHighlighted; in set 高亮
UIControlStateDisabled; 禁用
UIControlStateSelected; 中
UIControlStateApplication; plication use当应用程序标志使用时
UIControlStateReserved; 为内部框架预留的
};
typedefNSUInteger UIControlState;
(3)当按钮高亮或者禁用,UIButton类可以调整自己的外观,下面几个属性可以让你按照需要对按钮的外观进行微调。
adjustsImageWhenHighlighted: 默认情况下,在按钮被禁用时,图像会被画的颜色深些;
adjustsImageWhenDisabled: 默认情况下,按钮在被禁用时,图像会被画的颜色深一些;
showsTouchWhenHighlighted: 这个属性设置为YES,可使按钮在按下时发光。
2、重写绘制行为
backgroundRectForBounds: 指定背景边界;
contentRectForBounds: 指定内容边界;
titleRectForContentRect: 指定文字标题边界;
imageRectForContectRect: 指定按钮图像边界。
3、添加动作
-addTarget: action: forControlEvents:
转自《蓝鸥科技iOS开发指南》