美文网首页
🌹输入框中有无内容时Button的颜色变化🌹

🌹输入框中有无内容时Button的颜色变化🌹

作者: JaneEyre3X | 来源:发表于2018-10-02 23:37 被阅读0次

#import "ViewController.h"
#import "MyCellCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UITextField * text;
    UIButton * bt;
}
@property (nonatomic,strong) UIView *headerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    text = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 30)];
    text.backgroundColor =[UIColor grayColor];
    [self.view addSubview:text];
    
    
    bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bt.frame = CGRectMake((self.view.frame.size.width-80)/2, 150, 80, 30);
    [bt setTitle:@"登陆" forState:UIControlStateNormal];
    [bt setTitleColor:[UIColor redColor] forState:0];
    bt.alpha = 0.5;
    bt.userInteractionEnabled = NO;
    [bt addTarget:self action:@selector(Btn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bt];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:UITextFieldTextDidChangeNotification object:text];
}
-(void)change:(NSNotification *)notification
{
    if (text.text.length > 0) {
        bt.userInteractionEnabled = YES;
        [bt setTitleColor:[UIColor redColor] forState:0];
        bt.alpha = 1;
//        NSLog(@"内容大于0可以点击");
    }else
    {
        bt.userInteractionEnabled = NO;
        [bt setTitleColor:[UIColor redColor] forState:0];
        bt.alpha = 0.5;
//        NSLog(@"内容为空不可点击");
    }    
}
-(void)Btn:(UIButton * )sender
{
    if (text.text.length > 0)
    {
    
        NSLog(@"内容大于0可以点击");
    }
    else
    {
        
        NSLog(@"内容为空不可点击");
    }
}
@end
内容.gif

相关文章

网友评论

      本文标题:🌹输入框中有无内容时Button的颜色变化🌹

      本文链接:https://www.haomeiwen.com/subject/dqtzoftx.html