iOS中有关UIButton的方法

381 阅读1分钟

一、设置自定义navigationItem左右自定义按钮

1.直接设置左或者右边的按钮

UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
    [rightBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName,[UIColor redColor],NSForegroundColorAttributeName,nil] forState:(UIControlStateNormal)];
        // 字体大小
    self.navigationItem.rightBarButtonItem = rightBtn;

感受:使用[NSDictionary dictionaryWithObjectsAndKeys:属性值,属性名,nil]的方式比分步骤更加快。

2.分步设置

-(void)setNavi{
        UIButton * rightBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(0, 0, 20, 20);
        [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [rightBtn setTitleColor:ColorWithRGBHex(0xFF8415) forState:UIControlStateNormal];
        rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [rightBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

二、设置按钮的渐变颜色

-(UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveBtn.frame =CGRectMake(15, 40, ScreenWidth - 30, 44);
        _saveBtn.backgroundColor = ColorWithRGBHex(0x3296FD);
        CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
            gradientLayer.frame = CGRectMake(0, 0, ScreenWidth , 44);
            gradientLayer.startPoint = CGPointMake(0, 0);
            gradientLayer.endPoint = CGPointMake(1, 0);
            gradientLayer.locations = @[@(0),@(1.0)];//渐变点
        [gradientLayer setColors:@[(id)[[UIColor colorWithRed:50/255.0 green:150/255.0 blue:253/255.0 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:58/255.0 green:114/255.0 blue:253/255.0 alpha:1.0] CGColor]]];//渐变数组
        [_saveBtn.layer addSublayer:gradientLayer];
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            //        _saveBtn.layer.cornerRadius = 4;
       // _saveBtn.layer.masksToBounds = YES;
    }
    return _saveBtn;
}