<分区>
我的问题很简单。在 android 中,我们可以将 xml 样式表从布局中分离出来,以便它可以在任何地方重复使用,并且可以针对 UI 设计更改轻松进行编辑。
在 iOS xcode 中也可以吗?如果可以如何(如果不是来自 Controller ,则更喜欢)?需要图书馆吗?什么是好的图书馆?
感谢您的回答。
<分区>
我的问题很简单。在 android 中,我们可以将 xml 样式表从布局中分离出来,以便它可以在任何地方重复使用,并且可以针对 UI 设计更改轻松进行编辑。
在 iOS xcode 中也可以吗?如果可以如何(如果不是来自 Controller ,则更喜欢)?需要图书馆吗?什么是好的图书馆?
感谢您的回答。
最佳答案
您可以使用枚举创建自己的样式。通过将枚举放在 Styles 枚举中,您可以获得一个很好的分组:
enum Styles {
enum Labels {
case Standard
case LargeText
func style(label: UILabel) {
switch self {
case .Standard:
label.font = UIFont.systemFontOfSize(12)
case .LargeText:
label.font = UIFont.systemFontOfSize(18)
}
}
}
enum Buttons {
case RedButton
func style(button: UIButton) {
switch self {
case .RedButton:
button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
}
}
}
}
然后你可以像这样使用它:
Styles.Labels.Standard.style(yourLabel)
您还可以对已设置的样式进行扩展:
extension UILabel {
func style(style: Styles.Labels) {
style.style(self)
}
}
extension UIButton {
func style(style: Styles.Buttons) {
style.style(self)
}
}
然后像这样使用扩展:
yourLabel.style(.Standard)
yourButton.style(.RedButton)
关于ios - iOS 应用程序的有效 UI 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501816/