如何使用SingleViewApplication iOS创建标签栏?

时间:2016-12-10 06:41:07

标签: ios swift uiviewcontroller uitabbar uitabbaritem

我是iOS的新手,我的要求是实现Tab栏。 我有5个标签栏项目,每个项目有不同的屏幕(UIViewController)。 我有一个HomeViewController和剩下的4个ViewControllers。 我必须使用tabBar项显示所有UIViewControllers。 我必须在所有UIViewControllers的底部显示标签栏。

在图像中你可以看到。

在Image中,我在HomeViewController中,对于每个选项卡,我必须为各自的ViewControllers选择,但所有ViewControllers都应该显示标签栏。 StoryBoard: - enter image description here  我阅读了教程,但仍然感到困惑。 我该怎么办?

由于

1 个答案:

答案 0 :(得分:0)

您可以像这样创建选项卡视图控制器类

class TabBarViewController: UITabBarController {

    required init(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)!

override func viewDidLoad() {
    super.viewDidLoad()

    UITabBar.appearance().tintColor = UIColor(red: 254/255.0, green: 105/255.0, blue: 51/255.0, alpha: 100.0)

    for i in 0 ..< self.tabBar.items!.count {

        switch i {

        case 0:

            tabBar.items?[0].title = "Home"
            let firstTab = self.tabBar.items![i] as UITabBarItem
            learnTab.image = UIImage(named: "Homedeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            firstTab.selectedImage = UIImage(named: "Homedeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            firstTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            firstTab.titlePositionAdjustment = UIOffsetMake(0, -4)


        case 1:

            tabBar.items?[1].title = "Request"
            let secondTab = self.tabBar.items![i] as UITabBarItem
            secondTab.image = UIImage(named: "RequestDeselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            secondTab.selectedImage = UIImage(named: "Requestselected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            secondTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            secondTab.titlePositionAdjustment = UIOffsetMake(0, -4)

        case 2:

            tabBar.items?[2].title = "Renew"
            let thirdTab = self.tabBar.items![i] as UITabBarItem
             thirdTab.image = UIImage(named: "RenewDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            thirdTab.selectedImage = UIImage(named: "RenewSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            thirdTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            thirdTab.titlePositionAdjustment = UIOffsetMake(0, -4)

        case 3:

            tabBar.items?[3].title = "ReSell"
            let forthTab = self.tabBar.items![i] as UITabBarItem
            forthTab.image = UIImage(named: "ReSellDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            forthTab.selectedImage = UIImage(named: "ReSellSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            forthTab.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0)
            forthTab.titlePositionAdjustment = UIOffsetMake(0, -4)

        case 4:

            tabBar.items?[4].title = "ReCycle"
            let fifthTab = self.tabBar.items![i] as UITabBarItem
            fifthTab.image = UIImage(named: "ReCycleDeSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            fifthTab.selectedImage = UIImage(named: "ReCycleSelected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            fifthTab.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0)
            fifthTab.titlePositionAdjustment = UIOffsetMake(0, -5)

        default:
            break
        }
    }
}

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

    let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
    rootView.popToRootViewControllerAnimated(ture)


    }

}

}