ios - iOS 应用程序的有效 UI 样式

标签 ios swift ui-design

<分区>

我的问题很简单。在 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/

相关文章:

ios - 在 UITextView 中保存数据

ios - 我可以在创建 Cocoa Touch Framework 时使用 CocoaPods 吗?

ios - CoreData - 数据库损坏后重新创建持久存储

ios - 为什么我们需要将 sqlite DB 文件移动到文档文件夹?我们可以从 bundle 访问并对其执行数据库操作吗?

ios - 自定义 tabBar 的边框渐变

java - 建议设计 UI 屏幕以生成 XML 的方法

ios - 为什么需要 IOS 开发人员证书才能在设备上运行 IOS 应用程序?

objective-c - 在 Objective-C 中调用 Swift 闭包

swift - iOS8 Swift 中的 UIPickerView 宽度

安卓用户界面设计理念