UIButton

来源:互联网 发布:金十数据dora是谁 编辑:程序博客网 时间:2024/06/05 04:13
    //Button
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [_window makeKeyAndVisible];
    UIView *view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = [UIColor whiteColor];
    [_window addSubview:view];
    
    _button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(50, 50, 200, 50);
    _button.backgroundColor = [UIColor cyanColor];
    //给button设置抬头
    [_button setTitle:@"按我" forState:UIControlStateNormal];
    
//  [_button setTitle:@"aaa" forState:UIControlStateHighlighted];
    [_button setTitle:@"selected" forState:UIControlStateSelected];
        _button.selected = YES;
    //设置圆角
    _button.layer.cornerRadius = 10;      //layer 属性的属性
    
    //加边框颜色
    _button.layer.borderColor = [UIColor blackColor].CGColor;
    //边框线宽
    _button.layer.borderWidth = 0.5;
    //
    _button.titleLabel.font = [UIFont systemFontOfSize:Fontsize];
    
    //标记
    _button.tag = 101;
    
    //设置抬头颜色
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //设置阴影颜色
    [_button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
    
    //给button添加事件
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
   
    [view addSubview:_button];
    
#pragma mark button的响应事件
                           //参数
-(void)buttonAction:(UIButton *)sender{
    
    //第1中方法(标记,父类)
//    [(UIView *)(_window.subviews[0]) viewWithTag:101].backgroundColor = [UIColor redColor];
    //第2中方法 属性
//    _button.backgroundColor = [UIColor yellowColor];
   //第3中方法  (调参数)
    //sender.backgroundColor = [UIColor yellowColor];
    
    //
    CGFloat num1 = arc4random()%(255 - 1 + 1) + 1;
    CGFloat num2 = arc4random()%(255 - 1 + 1) + 1;
    CGFloat num3 = arc4random()%(255 - 1 + 1) + 1;
    sender.backgroundColor = [UIColor colorWithRed:num1/255.0 green:num2/255.0 blue:num3/255.0 alpha:1];
}

0 0
原创粉丝点击