如何在iOS中根据不同的用户类型动态切换UI主题

时间:2019-07-24 08:23:32

标签: ios swift design-patterns swift4.2 xcode10.1

在我的应用程序中,我有一个场景,需要根据用户类型切换UI主题设计。例如:在我的Type1用户流中,它像Registration Screen -> HomePage Screen,而在我的Type 2用户流中,它应该像Registration Screen-> Contact Screen -> Home Page Screen。在类型2用户的情况下,UI设计和主题也有所不同。为此,下面是我当前实现的示例代码流。

RegistrationViewController

(此视图可用于两个用户,但UI主题不同,例如导航栏颜色,bg颜色,按钮颜色,字体,图像等)

override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }


private func setupViews(){

        if Utilities.isUserType1{
            setupViewsForType1User() //Adds themes for type 1 user
        } else {
            setupViewsForType2User() //Adds themes for type 2 user
    }

}

 @IBAction func continueAction(_ sender: Any) {

    if Utilities.isUserType1{
            goToContactView() //Goes to ContactViewController
        } else {
            gotToHomeView() //Goes to HomeViewController
    }

}

ContactViewController (此视图仅适用于1类用户)

override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }


private func setupViews(){

        //Setupviews 

}

 @IBAction func continueAction(_ sender: Any) {

            gotToHomeView()

}

HomeViewController (该视图可用于两个用户,但UI主题与“注册”中所述不同)

override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }



private func setupViews(){

        if Utilities.isUserType1{
            setupViewsForType1User()
        } else {
            setupViewsForType2User()
    }

}

这很好用,但是这里的问题是我现在在实用程序中定义了一个isUserType,并且它的扩展性还不够好。对于每个流程和UI更改,我都需要基于此参数设置if-else条件。因此,现在,如果我将来有其他用户类型需要添加,那么我将再次需要另一个if-else语句,并根据该语句切换UI和流程。

有没有更好的方法来解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以检查更改主题 there

您需要根据不同的用户更改动态主题,以便发布通知并根据需要应用主题

// We create a model
struct Theme {
    let theme: String
    let fontColor: UIColor
    let alpha: CGFloat
}

// We need a protocol for we don't want all view controller listen theme
protocol Themeable: class {
    func listenTheme()
    func didThemeChange(theme: Theme)
}


// Global notification name
let themeableNotificationName = Notification.Name(rawValue: "ThemeableNotification")

// Our protocol extension and observer notification
extension Themeable where Self: UIViewController {
    func listenTheme() {
        NotificationCenter.default.addObserver(forName: themeableNotificationName, object: nil, queue: nil) { [weak self] notification in
            guard let theme = notification.object as? Theme else { return }
            self?.didThemeChange(theme: theme)
        }
    }

}


// Notification sender themeController
class NotifyThemeController: UIViewController {


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // Create a model and post
        NotificationCenter.default.post(name: themeableNotificationName, object: Theme(theme: "Lorem", fontColor: .red, alpha: 1.0), userInfo: nil)
    }

}

// YViewController
class YViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // We need call this method for observer
        listenTheme()
    }


}
// YViewController conforms Themeable
extension YViewController: Themeable {
    func didThemeChange(theme: Theme) {
        // TODO UI
    }

}
// ZViewController
class ZViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // We need call this method for observer
        listenTheme()
    }


}
// ZViewController conforms Themeable
extension ZViewController: Themeable {
    func didThemeChange(theme: Theme) {
        // TODO UI
    }

}

玩得开心!