【问题标题】:How to add MDCTabBar in the custom view?如何在自定义视图中添加 MDCTabBar?
【发布时间】:2023-04-06 22:30:01
【问题描述】:

我在 iOS 中使用 Material Design 库,我试图在导航栏下方的自定义视图中添加 MDCTabBar 但它不起作用。代码就像

let tabBar = MDCTabBar(frame: self.mainTabBar.bounds)
    tabBar.items = [
        UITabBarItem(title: "TAB 1", image: nil, tag: 0),
        UITabBarItem(title: "TAB 2", image: nil, tag: 1),
    ]
    tabBar.tintColor = UIColor.white
    tabBar.barTintColor = UIColor.theme
    tabBar.alignment = .justified
    tabBar.itemAppearance = .titles
    tabBar.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
    tabBar.displaysUppercaseTitles = true
    tabBar.sizeToFit()
    self.mainTabBar.addSubview(tabBar)

这里mainTabBar 是我的自定义视图,它正好在导航栏的下方。请帮忙解决这个问题。

提前致谢!

【问题讨论】:

    标签: ios material-design material-components material-components-ios


    【解决方案1】:

    您的 ViewController 类必须继承自 MDCTabBarViewController 类,例如:

        class SelectTeamViewController: MDCTabBarViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
    }
    

    那么它应该是可见的。 您甚至可以在情节提要中拖动 UIView 并在 Identity Inspector 中选择 MDCTabBar 类

    【讨论】:

      【解决方案2】:
      let tabBar = MDCTabBar(frame: self.mainTabBar.bounds)
      tabBar.delegate = self
      tabBar.items = [
              UITabBarItem(title: "Tab 1", image: nil, tag: 0),
              UITabBarItem(title: "Tab 2", image: nil, tag: 1),
          ]
      tabBar.tintColor = UIColor.white
      tabBar.barTintColor = UIColor.theme
      tabBar.alignment = .justified
      tabBar.itemAppearance = .titles
      tabBar.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
      tabBar.displaysUppercaseTitles = true
      tabBar.sizeToFit()
      self.mainTabBar.addSubview(tabBar)
      

      这里mainTabBar 是我的自定义视图,正好在 NavigationBar 下方。

      【讨论】:

      • 您好,先生!在自定义视图中自定义 MDCTabBar 之后,那么如何在特定的 tabBarItem 中显示特定的 viewController ???
      【解决方案3】:

      斯威夫特 4:

      我没有看到我的 tabBar 有两个原因!

      • 第一个事实是我没有选择与容器 (tabBarView) 颜色不同的颜色,因此它是白色 tabBar,白色 tabBarView 上有白色文本。
      • 第二个是一件棘手的事情,与其他以前的答案不同。我将 MDCTabBar 框架设置为等于 view.bounds ,然后使用 sizeToFit 方法将其压缩到容器 tabBarView 的大小。这是唯一让我在屏幕上处于正确位置的东西!欢迎您调整一些属性并在您的代码中使用它!阅读您的 cmets 会很有趣!

        func setTabBar() {
        
        // Set our MDCTabBar frame equal to view.bounds
        let tabBar = MDCTabBar(frame: view.bounds)
        tabBar.delegate = self
        tabBar.items = [
            UITabBarItem(title: NSLocalizedString("FirstTab", comment: ""), image: nil, tag: 0),
            UITabBarItem(title: NSLocalizedString("SecondTab", comment: ""), image: nil, tag: 1)
        ]
        tabBar.itemAppearance = .titles
        tabBar.barTintColor = .yellow
        tabBar.tintColor = .green
        tabBar.setTitleColor(.black, for: .normal)
        tabBar.setTitleColor(.blue, for: .selected)
        tabBar.displaysUppercaseTitles = false
        tabBar.alignment = .justified
        
        // This sizeToFit will squash our MDCTabBar to tabBarView size
        tabBar.sizeToFit()
        // Add MDCTabBar to our tabBarView as a subview
        tabBarView.addSubview(tabBar)
        

        }

      【讨论】:

      • 您好,先生!在自定义视图中自定义 MDCTabBar 之后,那么如何在特定的 tabBarItem 中显示特定的 viewController ???
      猜你喜欢
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      相关资源
      最近更新 更多