1、作用
- UIButtonConfiguration 是 iOS 15.0 新增的设置按钮 UI 元素的对象。
- 之前有过的设置 navigationBar 背景色和 tabBar 的背景色失效,都是因为新版本需要新增相应的 Configuration 对象才能正确显示。
- 新增 Configuration 对象设置 navigationBar 的背景色 和 tabBar 的背景色可以看我之前写的博客
- iOS 13 navigationBar的背景色失效
- iOS13 tabBar 设置背景色失效
- 新增 Configuration 对象设置 UI 的这种设计模式就是23种设计模式中的装饰模式。
2、UIButtonConfiguration作用的UI元素
- UIButtonConfiguration既然是装饰模式的一种实践,那么它必然只会对UI元素做集成,像消息传递、手势、动作、它是不参与的,颜色文本图片背景样式的,它都可以涉及到。
3、具体设置直接看代码
- oc 版本
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor systemPinkColor];
if (@available(iOS 15.0, *)) {
//初始化方法有多种,这里直接贴出需要的方法
//生成扁平纯净的ButtonConfiguration,不设置额外属性
UIButtonConfiguration *btnConfig = [UIButtonConfiguration plainButtonConfiguration];
//内置设置了一个圆角为5左右的ButtonConfiguration,但优先级小于btn.layer.cornerRadius
// UIButtonConfiguration *btnConfig = [UIButtonConfiguration borderedButtonConfiguration];
//圆角个性化ButtonConfiguration,主要体现在背景色不透明,文字和图片着色和背景色相反(例如黑色背景,白色文字或者白色背景,黑色文字等)。这一前提在于不主动设置文字颜色和图片颜色的前提。一般情况下,如果不设置文字和图片颜色,它们的颜色则为系统默认的系统蓝色。
// UIButtonConfiguration *btnConfig = [UIButtonConfiguration borderedProminentButtonConfiguration];
//设置文字
//标题
btnConfig.title = @"text";
//子标题
btnConfig.subtitle = @"subText";
//背景颜色(plainButtonConfiguration 模式下没有生效)
btnConfig.baseBackgroundColor = [UIColor redColor];
//文字颜色
btnConfig.baseForegroundColor = [UIColor yellowColor];
btnConfig.image = [UIImage imageNamed:@"administer_icon_invite"];
// 图片的位置(默认图片在左边)
btnConfig.imagePlacement = NSDirectionalRectEdgeLeading;
//内边距(内容四边距)
// btnConfig.contentInsets = NSDirectionalEdgeInsetsMake(50, 50, 50, 50);
//图标距文本的距离
btnConfig.imagePadding = 10;
//这个是一个关于background相关的config对象,他能处理的内容太多了,背景色,圆角,边框,阴影,自定义绘图(贝塞尔曲线绘图等),毛玻璃特效,背景图片等。只要和背景相关的UI元素,基本都能处理。
UIBackgroundConfiguration *backgroundConfig = [UIBackgroundConfiguration clearConfiguration];
//设置背景色(优先级最高)
backgroundConfig.backgroundColor = [UIColor grayColor];
//设置圆角半径(如果设置了 btn.layer.cornerRadius,会优先执行这行代码)
backgroundConfig.cornerRadius = 20;
btnConfig.background = backgroundConfig;
//标题与子标题的间距,可以为负值
// btnConfig.titlePadding = 20;
//创建按钮
UIButton *btn = [UIButton buttonWithConfiguration:btnConfig primaryAction:nil];
btn.frame = CGRectMake(100, 100, 200, 300);
btn.backgroundColor = [UIColor greenColor];
//btnConfig 设置了image,此时这里设置无效
btn.imageView.image = [UIImage imageNamed:@"1"];
// btn.layer.cornerRadius = 50;
// btn.layer.masksToBounds = YES;
btn.imageView.backgroundColor = [UIColor blueColor];
btn.titleLabel.backgroundColor = [UIColor blackColor];
//设置按钮内容整体水平居右
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[self.view addSubview:btn];
//根据按钮不同的状态切换不同的buttonConfiguration
btn.configurationUpdateHandler = ^(UIButton *b) {
if (b.state == UIControlStateHighlighted) {
btnConfig.showsActivityIndicator = YES;
btnConfig.attributedTitle = [[NSAttributedString alloc]initWithString:@"Highlighted Title" attributes:@{NSForegroundColorAttributeName:[UIColor systemRedColor]}];
btnConfig.attributedSubtitle = [[NSAttributedString alloc]initWithString:@"Highlighted Subtitle" attributes:@{NSForegroundColorAttributeName:[UIColor systemRedColor]}];
btnConfig.image = [UIImage systemImageNamed:@"square.and.arrow.up.fill"];
btnConfig.imageColorTransformer = ^UIColor * _Nonnull(UIColor * _Nonnull color) {
return [UIColor systemPurpleColor];
};
///这个赋值操作必须写,不然不生效
b.configuration = btnConfig;
} else {
btnConfig.showsActivityIndicator = NO;
btnConfig.attributedTitle = [[NSAttributedString alloc]initWithString:@"Normal Title" attributes:@{NSForegroundColorAttributeName:[UIColor systemRedColor]}];
btnConfig.attributedSubtitle = [[NSAttributedString alloc]initWithString:@"Normal Subtitle" attributes:@{NSForegroundColorAttributeName:[UIColor systemRedColor]}];
btnConfig.image = [UIImage systemImageNamed:@"square.and.arrow.up"];
btnConfig.imageColorTransformer = ^UIColor * _Nonnull(UIColor * _Nonnull color) {
return [UIColor systemOrangeColor];
};
///这个赋值操作必须写,不然不生效
b.configuration = btnConfig;
}
};
} else {
UIButton *likeBtn = [[UIButton alloc]init];
likeBtn.frame = CGRectMake(50, 50, 100, 50);
[likeBtn setTitle:@"按钮" forState:UIControlStateNormal];
likeBtn.titleLabel.font = [UIFont systemFontOfSize:11.0];
likeBtn.backgroundColor = [UIColor redColor];
//设置按钮文字居右(无效)
// likeBtn.titleLabel.textAlignment = NSTextAlignmentRight;
//设置按钮文字颜色
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置按钮整体居右
likeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
likeBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
likeBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5);
[self.view addSubview:likeBtn];
}
}
- Swift 版本
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let btn: UIButton = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 100))
btn.backgroundColor = .green
btn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
if #available(iOS 15.0, *) {
//初始化 configuration
btn.configuration = UIButton.Configuration.plain()
//设置字体颜色
btn.configuration?.baseForegroundColor = .red
//设置背景色(plain初始化模式下失效)
btn.configuration?.baseBackgroundColor = .yellow
// title 和 subtitle的对其关系,文本是上下排版的
btn.configuration?.titleAlignment = .trailing
//标题
btn.configuration?.title = "title"
//子标题
btn.configuration?.subtitle = "subTitle"
//title 和 subtitle的间距 (可以为负)
btn.configuration?.titlePadding = 10
// image 和 文本 的相对位置
btn.configuration?.imagePlacement = .trailing
//image 和 文本的间距
btn.configuration?.imagePadding = 10;
//button的内容(title,subtitle,image)显示后与按钮的边距,默认有一段距离
btn.configuration?.contentInsets = NSDirectionalEdgeInsets.zero
// 设置按钮的状态变化的监听,根据变化来改变按钮的显示内容
btn.configurationUpdateHandler = { (button: UIButton) -> Void in
switch button.state {
case .normal: //默认状态下
//设置image
button.configuration?.image = UIImage(named: "administer_icon_invite")
//背景色
button.configuration?.background.backgroundColor = .blue
// 设置字体
button.configuration?.attributedTitle = AttributedString("resetTitle", attributes: AttributeContainer([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.black]))
case .highlighted: //高亮状态下
//设置image
button.configuration?.image = UIImage(named: "shoucang_sel")
//背景色
button.configuration?.background.backgroundColor = .gray
// 设置字体
button.configuration?.attributedTitle = AttributedString("highlightedTitle", attributes: AttributeContainer([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.orange]))
default:
print("默认状态: \(button.state)")
}
button.updateConfiguration()
}
} else {
//iOS 15 之前的属性设置
//按钮的背景色
btn.backgroundColor = .yellow
btn.setTitle("按钮", for: .normal)
btn.setTitleColor(UIColor.red, for: .normal)
btn.setTitleColor(.orange, for: .highlighted)
btn.setImage(UIImage(named: "shoucang_sel"), for: .normal)
//设置字体大小
btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
// 改变title与image的位置关系
btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 5)
btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: -10)
//设置按钮内容整体水平居右
btn.contentHorizontalAlignment = .trailing
}
view.addSubview(btn)
}
@objc func btnClicked() {
print("按钮点击了")
}
}
文章来源地址https://www.toymoban.com/news/detail-621726.html
文章来源:https://www.toymoban.com/news/detail-621726.html
到了这里,关于iOS UIButtonConfiguration的简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!