您的位置:首页 > 产品设计 > UI/UE

iOS之UIButton基本用法

2016-03-02 17:24 323 查看
#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UIButton *btn;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//初始化按钮,设置按钮类型
self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
/*
Type:
UIButtonTypeCustom = 0, // 自定义类型
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 系统类型
UIButtonTypeDetailDisclosure,//详细描述样式,圆圈中间加个i
UIButtonTypeInfoLight, //浅色的详细描述样式
UIButtonTypeInfoDark,//深色的详细描述样式
UIButtonTypeContactAdd,//加号样式
UIButtonTypeRoundedRect = UIButtonTypeSystem,   // 圆角矩形
*/

//设置按钮位置和尺寸
self.btn.frame = CGRectMake(50, 50, 300, 50);

//设置按钮文字标题
[self.btn setTitle:@"我是一个按钮" forState:UIControlStateNormal];
/*
State:前三个较为常用
UIControlStateNormal //正常状态下
UIControlStateHighlighted //高亮状态下,按钮按下还未抬起的时候
UIControlStateDisabled  //按钮禁用状态下,不能使用
UIControlStateSelected  //选中状态下
UIControlStateApplication //当应用程序标志时
UIControlStateReserved  //为内部框架预留
*/

//设置按钮文字颜色
[self.btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

//设置背景图片(需要注意按钮类型最好为自定义,系统类型的会使图片变暗)
//    [self.btn setImage:[UIImage imageNamed:@"tupian"] forState:UIControlStateNormal];

//设置按钮文字大小
self.btn.titleLabel.font = [UIFont systemFontOfSize:20];

//设按钮背景颜色
self.btn.backgroundColor = [UIColor cyanColor];

//设置按钮文字阴影颜色
[self.btn setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateNormal];

//默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,可以将这个属性设置为NO
self.btn.adjustsImageWhenHighlighted = NO;

//默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,可以将这个属性设置为NO
self.btn.adjustsImageWhenDisabled = NO;

//下面的这个属性设置为yes的状态下,按钮按下会发光,这可以用于信息按钮或者有些重要的按钮
self.btn.showsTouchWhenHighlighted = YES;

//按钮响应点击事件,最常用的方法:第一个参数是目标对象,一般都是self; 第二个参数是一个SEL类型的方法;第三个参数是按钮点击事件
[self.btn addTarget:self action:@selector(Method) forControlEvents:UIControlEventTouchUpInside];

//将控件添加到当前视图上
[self.view addSubview:self.btn];

}

- (void)Method
{
NSLog(@"学东哥哥随笔~~~~");
}

/*如果自定义按钮类,可以重写下面方法,定制自己需要的按钮

- (CGRect)backgroundRectForBounds:(CGRect)bounds; //指定背景边界
- (CGRect)contentRectForBounds:(CGRect)bounds; //指定内容边界
- (CGRect)titleRectForContentRect:(CGRect)contentRect; //指定文字标题边界
- (CGRect)imageRectForContentRect:(CGRect)contentRect; //指定按钮图像边界

*/

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: