1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @property (strong, nonatomic) UIButton *btn; 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 14 //初始化按钮,设置按钮类型 15 self.btn = [UIButton buttonWithType:UIButtonTypeSystem]; 16 /* 17 Type: 18 UIButtonTypeCustom = 0, // 自定义类型 19 UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // 系统类型 20 UIButtonTypeDetailDisclosure,//详细描述样式,圆圈中间加个i 21 UIButtonTypeInfoLight, //浅色的详细描述样式 22 UIButtonTypeInfoDark,//深色的详细描述样式 23 UIButtonTypeContactAdd,//加号样式 24 UIButtonTypeRoundedRect = UIButtonTypeSystem, // 圆角矩形 25 */ 26 27 //设置按钮位置和尺寸 28 self.btn.frame = CGRectMake(50, 50, 300, 50); 29 30 //设置按钮文字标题 31 [self.btn setTitle:@"我是一个按钮" forState:UIControlStateNormal]; 32 /* 33 State:前三个较为常用 34 UIControlStateNormal //正常状态下 35 UIControlStateHighlighted //高亮状态下,按钮按下还未抬起的时候 36 UIControlStateDisabled //按钮禁用状态下,不能使用 37 UIControlStateSelected //选中状态下 38 UIControlStateApplication //当应用程序标志时 39 UIControlStateReserved //为内部框架预留 40 */ 41 42 //设置按钮文字颜色 43 [self.btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; 44 45 //设置背景图片(需要注意按钮类型最好为自定义,系统类型的会使图片变暗) 46 // [self.btn setImage:[UIImage imageNamed:@"tupian"] forState:UIControlStateNormal]; 47 48 //设置按钮文字大小 49 self.btn.titleLabel.font = [UIFont systemFontOfSize:20]; 50 51 //设按钮背景颜色 52 self.btn.backgroundColor = [UIColor cyanColor]; 53 54 //设置按钮文字阴影颜色 55 [self.btn setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateNormal]; 56 57 //默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,可以将这个属性设置为NO 58 self.btn.adjustsImageWhenHighlighted = NO; 59 60 //默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,可以将这个属性设置为NO 61 self.btn.adjustsImageWhenDisabled = NO; 62 63 //下面的这个属性设置为yes的状态下,按钮按下会发光,这可以用于信息按钮或者有些重要的按钮 64 self.btn.showsTouchWhenHighlighted = YES; 65 66 //按钮响应点击事件,最常用的方法:第一个参数是目标对象,一般都是self; 第二个参数是一个SEL类型的方法;第三个参数是按钮点击事件 67 [self.btn addTarget:self action:@selector(Method) forControlEvents:UIControlEventTouchUpInside]; 68 69 //将控件添加到当前视图上 70 [self.view addSubview:self.btn]; 71 72 } 73 74 - (void)Method 75 { 76 NSLog(@"学东哥哥随笔~~~~"); 77 } 78 79 80 /*如果自定义按钮类,可以重写下面方法,定制自己需要的按钮 81 82 - (CGRect)backgroundRectForBounds:(CGRect)bounds; //指定背景边界 83 - (CGRect)contentRectForBounds:(CGRect)bounds; //指定内容边界 84 - (CGRect)titleRectForContentRect:(CGRect)contentRect; //指定文字标题边界 85 - (CGRect)imageRectForContentRect:(CGRect)contentRect; //指定按钮图像边界 86 87 */ 88 89 90 91 - (void)didReceiveMemoryWarning { 92 [super didReceiveMemoryWarning]; 93 // Dispose of any resources that can be recreated. 94 } 95 96 @end