UIButton设置

按钮继承自UIControl,所以其样式设置也跟UIView不一样!!

/创建导航栏右边按钮

UIButton* barButtonItemCancel = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮标题

[barButtonItemCancel setTitle:AppLocalizedString(@"Cancel") forState:UIControlStateNormal];

//设置按钮标题字体

barButtonItemCancel.titleLabel.font = [UIFont systemFontOfSize:12];

//设置按钮标题颜色

[barButtonItemCancel setTitleColor:kMainColorOfApp forState:UIControlStateNormal];

//设置按钮标题对齐样式

barButtonItemCancel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

//设置按钮背景色

[barButtonItemCancel setBackgroundColor:[UIColor redColor]];

[barButtonItemCancel addTarget:self action:@selector(onClickCancel) forControlEvents: UIControlEventTouchUpInside];

你可能感兴趣的:(UIButton设置)