UIButton相关

创建按钮对象

  • 直接创建
UIButton *button = [[UIButton alloc] init];
  • 创建并设置按钮类型(设置按钮的类型只能在初始化的时候设置)
/**
UIButtonType是枚举,枚举值如下:
UIButtonTypeCustom = 0,                         // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,

UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//不能通过这样的方式赋值,button.buttonType为只读
//button.buttonType = UIButtonTypeInfoDark;

设置背景颜色

button.backgroundColor = [UIColor redColor];

//或
[button setBackgroundColor:[UIColor redColor]];

设置文字

//不能通过如下方式赋值
//button.titleLabel.text = @"普通文字";
//因为按钮有几种状态,分别要对不同状态下的文字单独赋值
[button setTitle:@"普通按钮" forState:UIControlStateNormal];//正常状态下
[button setTitle:@"高亮按钮" forState:UIControlStateHighlighted];//选中状态下
[button setTitle:@"禁用按钮" forState:UIControlStateDisabled];//禁用状态下

设置颜色

//分别要对不同状态下的文字单独赋值
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];//正常状态下
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];//选中状态下
[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];//禁用状态下

设置文字的阴影颜色

//分别要对不同状态下的文字单独赋值
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];//正常状态下
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//选中状态下
[button setTitleShadowColor:[UIColor grayColor] forState:UIControlStateDisabled];//禁用状态下

设置内容图片

//分别要对不同状态下的内容图片单独赋值
[button setImage:[UIImage imageNamed:@"player_btn_normal"] forState:UIControlStateNormal];//正常状态下
[button setImage:[UIImage imageNamed:@"player_btn_highlight"] forState:UIControlStateHighlighted];//选中状态下
[button setImage:[UIImage imageNamed:@"player_btn_disabled"] forState:UIControlStateDisabled];//禁用状态下

设置背景图片

//分别要对不同状态下的内容图片单独赋值
[button setBackgroundImage:[UIImage imageNamed:@"player_btn_back_normal"] forState:UIControlStateNormal];//正常状态下
[button setBackgroundImage:[UIImage imageNamed:@"player_btn_back_highlight"] forState:UIControlStateHighlighted];//选中状态下
[button setBackgroundImage:[UIImage imageNamed:@"player_btn_back_disabled"] forState:UIControlStateDisabled];//禁用状态下

重新设置内容图片和文字的位置于大小

[button titleRectForContentRect:CGRectMake(0, 0, 100, 70)];
[button imageRectForContentRect:CGRectMake(100, 0, 70, 70)];

尺寸自适应

[button sizeToFit];

设置内容的内边距

button.contentEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);

设置图片的内边距

button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

设置文字的内边距

button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);

按钮添加事件

[button addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

...

- (void)butClick:(UIButton*)btn
{

}

results matching ""

    No results matching ""