DesignableButton 是一个 UIButton 子类,使用集中和可重用的样式。在 InterfaceBuilder 中,拖放一个 DesignableButton,选择您的样式,并将其立即应用于 InterfaceBuilder
功能
- 一个集中的类来定义您的主要按钮样式,以便可以重用
- 不同按钮状态的样式:正常/选中/禁用
- 集中的样式可以在 IB 中设置(不再需要 IBOutlets 进行编程式样式化)
- 实时在 IB 中查看样式。甚至将按钮切换到选中状态也会更新 IB
- 单个按钮可以在 IB 中覆盖它们的集中样式
- 随着 DesignableButton,可以轻松设置圆角和边框
- 带有图像在文本上方的按钮使用 DesignableButton 非常简单
如何安装
将此添加到您的 CocoaPods Podfile 中。注意 IH 前缀
pod 'IHDesignableButton'
如何使用
在您的项目中创建一个DesignableButton+Styles.swift扩展文件
import UIKit
import IHDesignableButton // if DesignableButton is in CocoaPod
extension DesignableButton {
override open func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
DesignableButton.setDesignableStyles(isInterfaceBuilder: true)
}
static func setDesignableStyles(isInterfaceBuilder: Bool = false) {
// example style
DesignableButton.setStyle(style: { (designableButton: DesignableButton) -> Void in
if designableButton.isHighlighted || designableButton.isSelected {
// replace this with your style for selected/highlighted states
designableButton.setTitleColor(designableButton.customTextColor ?? UIColor.white, for: UIControlState())
designableButton.backgroundColor = designableButton.selectedColor ?? UIColor.red
designableButton.layer.borderColor = designableButton.selectedColor?.cgColor ?? UIColor.red.cgColor
designableButton.layer.borderWidth = designableButton.borderWidth ?? 0
} else if designableButton.isEnabled {
// replace this with your style for the normal state
designableButton.setTitleColor(designableButton.customTextColor ?? UIColor.white, for: UIControlState())
designableButton.backgroundColor = designableButton.defaultColor ?? UIColor.blue
designableButton.layer.borderColor = designableButton.defaultColor?.cgColor ?? UIColor.blue.cgColor
designableButton.layer.borderWidth = designableButton.borderWidth ?? 0
}
else {
// replace this with your style for the disabled state
designableButton.setTitleColor(designableButton.customTextColor ?? UIColor.lightGray, for: UIControlState())
designableButton.backgroundColor = designableButton.disabledColor ?? UIColor.lightGray()
designableButton.layer.borderColor = designableButton.borderColor?.cgColor ?? UIColor.gray.cgColor
designableButton.layer.borderWidth = designableButton.borderWidth ?? 1
}
// replace this with your style for all states
designableButton.layer.cornerRadius = designableButton.cornerRadius ?? 12
designableButton.setTitle(designableButton.titleLabel?.text, for: .normal)
}, for: "primary") // this is the name/key of your style
}
}
在setDesignableStyles()
方法中,您可以调用DesignableButton.setStyle()
来创建新的样式。大多数项目仅需要一些按钮样式
在AppDelegate的application(_:didFinishLaunchingWithOptions)
中调用DesignableButton.setDesignableStyles()
现在在Interface Builder中,将一个UIButton拖放到视图中
将类更改为DesignableButton
,模块更改为IHDesignableButton
将按钮类型从System
更改为Custom
设置按钮样式为“primary”或您创建的任何其他按钮样式
限时特价 :
A Codable扩展,用于解析数组并记录所有解码失败情况 https://github.com/IdleHandsApps/SafeDecoder
当键盘弹出时保留视图可见性的优雅解决方案 https://github.com/IdleHandsApps/IHKeyboardAvoiding
用于无网络连接消息的完全自动状态栏覆盖 https://github.com/IdleHandsApps/StatusBarOverlay
作者
- Fraser Scott-Morrison ([email protected])
非常欢迎分享任何使用DesignableButton的酷应用程序
许可
分发许可为MIT