概述:
UIButton 类提供了按钮功能(将触摸按钮的状态信息发送给目标对象). UIButton的对象共有三种状态: normal(正常状态)/ selected(按下去时的状态)/ highlighted(被选中的状态).同时 UIButton 也提供了多种系统样式
1.1 属性方法
配置 title
forState:
(
UIControlState
)
state
forState:
(
UIControlState
)
state
forState:
(
UIControlState
)
state
forState:
(
UIControlState
)
state
@property
(
nonatomic
)
BOOL
reversesTitleShadowWhenHighlighted
|
现实的文本
在设定状态下,设置title
在设定状态下设置属性title
设置字体颜色
设置字体阴影
设置阴影颜色
|
配置 button的界面显示
@property
(
nonatomic
)
BOOL
showsTouchWhenHighlighted
forState:
(
UIControlState
)
state
forState:
(
UIControlState
)
state
|
按下按钮时, 按钮会闪一下
设置指定状态的图片
|
配置缩进
|
内容缩进
标题缩进
图片缩进
|
获取当前的状态
| 只读 |
获取尺寸
- (
CGRect
)
backgroundRectForBounds:
(
CGRect
)
bounds
- (
CGRect
)
contentRectForBounds:
(
CGRect
)
bounds
- (
CGRect
)
titleRectForContentRect:
(
CGRect
)
contentRect
- (
CGRect
)
imageRectForContentRect:
(
CGRect
)
contentRect
|
背景尺寸
内容尺寸
文本内容尺寸
图片尺寸
|
按钮样式
UIButtonTypeCustom
和
UIButtonTypeRoundedRect
的区别
UIButtonTypeCustom :是一个背景透明的按钮。 如果想做按钮中图片切换(不同状态),必须使用两张图片进行设置。 UIButtonTypeRoundedRect :是一个圆角矩形按钮。方便之处:系统会自动为这种按钮设置高亮状态(半透明状态)。即,通过一张图片,就可以做出按钮交互的效果。 不方便的地方:如果你想自定义不同状态下,按钮显示的背景,这种类型的按钮就支持的不好。
建议:快速开发,每个按钮只有一张图片,建议使用UIButtonTypeRoundedRect。按钮高度自定义,建议使用UIButtonTypeCustom。
|
按钮扩大触摸响应区域
原代码如下:
menuBtn.frame = CGRectMake(
4
,
8
,
44
,
28
);
扩大触摸区域代码如下:
[menuBtn setImage:[PYUtiles imageFromFile: @“MenuBtn.png” ] forState:UIControlStateNormal]; [menuBtn setFrame:CGRectMake( 0 , 0 , 48 , 44 )]; [menuBtn setContentMode:UIViewContentModeCenter]; 原理,扩大 button 的 frame rect ,并且将图片设置成居中即可
注意,这边要是setImage,setBackgroundImage不行,会扩大图片到整个响应区域
|