iOS Button 按钮 UIControlStateDisabled 状态的使用


//当为1时,按钮可以点击,当为2时按钮不可点击

//标红的地方需要同时设置


//  ViewController.m
//  ButtonDemo
//
//  Created by apple on 16/5/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"
#define kScreenBounds ([[UIScreen mainScreen] bounds])
#define kScreenwidth (kScreenBounds.size.width)
#define kScreenheight (kScreenBounds.size.height)
#define mBlueColor [UIColor colorWithRed:50.0/255.0 green:162.0/255.0 blue:248.0/255.0 alpha:1.0]
#define mGrayColor [UIColor colorWithRed:165/255.0 green:165/255.0 blue:165/255.0 alpha:1.0]

@interface ViewController ()

@property (nonatomic,strong)UIButton *button;
@property (nonatomic,strong)UIButton *button1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(kScreenwidth/2-50, kScreenheight/2-100, 100, 50)];
    [self.button1 setBackgroundColor:mBlueColor];
    [self.button1 setTitle:@"测试" forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(two) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button1];
    
    
    self.button = [[UIButton alloc]initWithFrame:CGRectMake(kScreenwidth/2-50, kScreenheight/2, 100, 50)];
    [self.button setBackgroundColor:mBlueColor];
    [self.button setBackgroundImage:[self createImageWithColor:mGrayColor] forState:UIControlStateDisabled];
    self.button.enabled = NO;
    [self.button setTitle:@"1" forState:UIControlStateNormal];
    [self.button setTitle:@"2" forState:UIControlStateDisabled];
    [self.button addTarget:self action:@selector(one) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
}

- (void)one{
    
}

- (void)two{
    self.button.enabled = !self.button.enabled;
    if (self.button.enabled) {
        [self.button setBackgroundColor:mBlueColor];
    }else{
        [self.button setBackgroundColor:mGrayColor];
    }
}

/** 用颜色生成一张图片 */
- (UIImage *)createImageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值