Search code examples
iosmaterial-designmaterial-componentsmaterial-components-ios

How to add MDCTabBar in the custom view?


I'm using Material Design library in iOS, I'm trying to add MDCTabBar in the custom view below the navigation bar but it doesn't work. Code is like

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)

Here mainTabBar is my custom view and it is exactly below the navigation bar. Please help to solve this.

Thanks in advance!


Solution

  • 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)
    

    Here mainTabBar is my customview which is exactly below the NavigationBar.