简体   繁体   English

在iOS中模拟标签栏

[英]Simulate a tab bar in iOS

I have many views on top on another in a view controller, and I'm trying to simulate a tab bar. 我在视图控制器中的另一个视图之上有许多视图,并且正在尝试模拟选项卡栏。 The user can tap on any button of a menu and a view will be presented according to which button was tapped. 用户可以点击菜单上的任何按钮,然后根据所点击的按钮显示视图。 All the other views will hide. 所有其他视图将隐藏。

I've written the following code, which doesn't present the views correctly (sometimes they show up, other times they don't). 我编写了以下代码,这些代码无法正确显示视图(有时会显示视图,有时则无法显示)。

presentViewWithName(.home) // this will present the view at the very bottom of the hierarchy

enum Views {
    case home
    case sketchpad
    case help
    case settings
}

func presentViewWithName(name: Views) {
    self.turnAlphaOnToEveryMenuButton()
    switch name {
    case .home:
        self.hideAllViews()
        self.menuView.buttonHome.dimmerAlpha()
    case .sketchpad:
        self.sketchpadView.animateToggleAlpha()
        self.menuView.buttonSketchpad.dimmerAlpha()
    case .help:
        self.helpView.animateToggleAlpha()
        self.menuView.buttonHelp.dimmerAlpha()
    case .settings:
        self.settingsView.animateToggleAlpha()
        self.menuView.buttonSettings.dimmerAlpha()
    }
}

func turnAlphaOnToEveryMenuButton() {
    self.menuView.buttonHome.alpha = 1
    self.menuView.buttonSketchpad.alpha = 1
    self.menuView.buttonHelp.alpha = 1
    self.menuView.buttonSettings.alpha = 1
}

func hideAllViews() {
    self.settingsView.alpha = 0
    self.sketchpadView.alpha = 0
    self.helpView.alpha = 0
}

extension UIView {
    func animateToggleAlpha() {
        UIView.animateWithDuration(0.25) {
            self.alpha = self.alpha == 1 ? 0 : 1
        }
    }

    func dimmerAlpha() {
        UIView.animateWithDuration(0.25) {
            self.alpha = self.alpha == 1 ? 0.4 : 1
        }
    }
}

I'm basically changing the alpha of the views. 我基本上是在更改视图的Alpha。 Say you want to present the home view. 假设您要显示主视图。 That's the view at the very bottom of the hierarchy. 这是层次结构最底层的视图。 I just hide all the views. 我只是隐藏所有意见。 Say I want to present the view at the very top of the hierarchy, the settings view. 假设我想将视图显示在层次结构的最顶层,即设置视图。 I hide all the views, and then present the settings view. 我隐藏所有视图,然后显示设置视图。

How can I fix this/better do it? 我该如何解决/做得更好?

There should be 4 views home, sketchpad, help and setting. 应该有4个视图,包括首页,画板,帮助和设置。 All your view should have same super view. 您所有的视图都应具有相同的超级视图。 It should be connected to same outlet menuViews as home, sketchpad, help and setting respectively, 它应分别连接到与首页,画板,帮助和设置相同的插座菜单视图,

@IBOutlet weak var menuViews: [UIView]! 

This is your enum, 这是你的枚举

enum Views {
   case home
   case sketchpad
   case help
   case settings
}

Add function to show that view, 添加功能以显示该视图,

func presentViewWithName(name: Views) {
    UIView.animateWithDuration(0.25) {
       for (index, view) in menuViews.enumerate() {
          let  alpha = (name == (Views(rawValue: index)!)) ? 1.0 : 0.0
          view.alpha = alpha
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM