OC-UI-004.纯代码编写按钮的简单方法

来源:互联网 发布:卡片战斗先导者g爱知 编辑:程序博客网 时间:2024/04/28 14:34

通过加载view的时候写入代码,进行控件编写

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController////view加载完毕就会调用这个方法- (void)viewDidLoad {    [super viewDidLoad]; //必须要让父类调用这个方法    UIButton *button = [[UIButton alloc] init];//创建一个按钮    UIImage *image1 = [UIImage imageNamed:@"btn_01"];//创建一张图片,PNG格式不用加后缀    [button setBackgroundImage:image1 forState:UIControlStateNormal];//设置按钮背景图    UIImage *image2 = [UIImage imageNamed:@"btn_02"];//创建第二张图    //设置背景高亮图--按钮点击下去    [button setBackgroundImage:image2 forState:UIControlStateHighlighted];    [button setTitle:@"点我呀" forState:UIControlStateNormal];//设置文字    //设置文字颜色    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [button setTitle:@"你妹的" forState:UIControlStateHighlighted];//设置高亮文字    //设置高亮文字颜色    [button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];    button.frame = CGRectMake(50, 50, 100, 100);//设置位置与尺寸    [self.view addSubview:button];//在父view 添加 按钮控件    //添加事件的监听    [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];        UIButton *buttonPlus = [UIButton buttonWithType:UIButtonTypeContactAdd];    buttonPlus.center = CGPointMake(100, 300);    [self.view addSubview:buttonPlus];    [buttonPlus addTarget:self action:@selector(move) forControlEvents:UIControlEventTouchUpInside];}-(void)show{    NSLog(@"老婆真好");}-(void)move{    NSLog(@"老公真好");}//- (void)didReceiveMemoryWarning {//    [super didReceiveMemoryWarning];//    // Dispose of any resources that can be recreated.//}@end


0 0