标签:
1、初始化
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(100, 100, 60, 60)];
[self.view addSubview:btn];
2、常用设置
设置文字、文字颜色、图片
[btn setTitle:@"盖" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
[optionButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 2, 2, 2)];//文字内边距
[btn1 setImage:[UIImage imageNamed:@"beer-cap"] forState:UIControlStateNormal];//image会覆盖文字
[self.imageButton setAdjustsImageWhenDisabled:NO];//自适应图片的大小
设置背景图、背景颜色
[btn1 setBackgroundImage:[UIImage imageNamed:@"beer-cap"] forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColor blueColor]];//背景图部分透明时配合使用
选中
[self.cuttentOptionButton setSelected:NO];
设置tag
[btn1 setTag:21];
UIButton * btn2 = [self.view viewWithTag:21];//根据tag获取
隐藏
button.hidden = YES;或 [button isHidden];
几个常用的只读属性,用于判断、赋值
NSString *currentTitle; // normal/highlighted/selected/disabled. can return nil
UIColor *currentTitleColor; // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)
UIColor *currentTitleShadowColor; // normal/highlighted/selected/disabled.
UIImage *currentImage; // normal/highlighted/selected/disabled. can return nil
UIImage *currentBackgroundImage; // normal/highlighted/selected/disabled. can return nil
NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // normal/highlighted/selected/disabled. can return nil
3、添加事件
[button addTarget:self action:@selector(btnMove:) forControlEvents:UIControlEventTouchUpInside];//带参,参数为按钮本身
标签:
原文地址:http://www.cnblogs.com/ininin/p/5767163.html