按钮UIButton

来源:互联网 发布:问卷星可以分析数据吗 编辑:程序博客网 时间:2024/05/16 18:49
/*_____________________________UIButton_____________________________*/- (void)_initButton {    //创建一个按钮//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 100, 60, 40)];//    button.buttonType = UIButtonTypeRoundedRect;  //不可以这么设置    //button只可以用过类方法设置类型    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button.frame = CGRectMake(220, 100, 60, 40);    //设置背景颜色//    button.backgroundColor = [UIColor purpleColor];        //设置标题,他会和状态绑定    //设置普通状态的标题    [button setTitle:@"按钮" forState:UIControlStateNormal];    //设置高亮状态的标题    [button setTitle:@"点击中" forState:UIControlStateHighlighted];    //设置标题文字的字体    button.titleLabel.font = [UIFont systemFontOfSize:19];    //设置标题//    button.titleLabel.text = @"text"; //不可取        //设置标题的字体颜色(和状态绑定)    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];        button.tag = 100;        //添加点击事件    /*     UIControlEventTouchUpInside:在里面点击在里面松开     UIControlEventTouchDragInside:按着拖动触发事件     UIControlEventTouchDragOutside:在里面点击拖动到外面触发方法     UIControlEventTouchUpOutside:在里面点击后在外面松开     */    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];        //使用系统样式的按钮    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeInfoLight];    button1.tag = 101;    button1.frame = CGRectMake(220, 160, 40, 40);    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button1];    }- (void)buttonAction:(UIButton *)button {    if (button.tag == 100) {         NSLog(@"按钮button被点击了");    }else if (button.tag == 101) {        NSLog(@"按钮button1被点击了");    }else if (button.tag == 102) {        NSLog(@"自定义按钮被点击了");        button.selected = !button.selected;    }}//设置自定义图片的按钮- (void)_initCustomButton {    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(220, 200, 80, 40);        button.tag = 102;        //创建显示的图片    UIImage *image1 = [UIImage imageNamed:@"back_on_black"];    UIImage *image2 = [UIImage imageNamed:@"back_on"];        //设置标题    [button setTitle:@"按钮" forState:UIControlStateNormal];    [button setTitle:@"点击中" forState:UIControlStateHighlighted];        //设置按钮的显示图片(注意:如果向设置标题,则用此种方法不能设置标题)//    [button setImage:image1 forState:UIControlStateNormal];//    [button setImage:image2 forState:UIControlStateHighlighted];        //设置标题的同时设置图片可以用以下方法    [button setBackgroundImage:image1 forState:UIControlStateNormal];    [button setBackgroundImage:image2 forState:UIControlStateSelected];        //添加点击事件    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];        [button setTitle:@"禁用" forState:UIControlStateDisabled];        //设置按钮的禁用属性    button.enabled = NO;        [self.view addSubview:button];    }

0 0
原创粉丝点击