最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]搞懂swift写tab的标准写法

Swift crifan 3761浏览 0评论
目前,swift写了个tab页面,是这样的:
       //6. create tabs
        let viewConversation    = ConversationViewController()
        let viewContact         = ContactViewController()
        let viewFile            = FileViewController()
        let viewFavorite        = FavoriteViewController()
        let viewMy              = MyViewController()

        let nvcConversation = UINavigationController(rootViewController: viewConversation)
        nvcConversation.tabBarItem = UITabBarItem(title: mainTabs[0], image: UIImage(named:"tab_conversation"), tag:0)
        nvcConversation.tabBarItem.selectedImage = UIImage(named: "tab_conversation_selected")
        //nvcConversation.tabBarItem.badgeValue = "3"

        let nvcContact = UINavigationController(rootViewController: viewContact)
        nvcContact.tabBarItem = UITabBarItem(title: mainTabs[1], image: UIImage(named:"tab_contact"), tag:1)
        nvcContact.tabBarItem.selectedImage = UIImage(named: "tab_contact_selected")

        let nvcFile = UINavigationController(rootViewController: viewFile)
        nvcFile.tabBarItem = UITabBarItem(title: mainTabs[2], image: UIImage(named:"tab_file"), tag:2)
        nvcFile.tabBarItem.selectedImage = UIImage(named: "tab_file_selected")

        let nvcFavorite = UINavigationController(rootViewController: viewFavorite)
        nvcFavorite.tabBarItem = UITabBarItem(title: mainTabs[3], image: UIImage(named:"tab_favorite"), tag:3)
        nvcFavorite.tabBarItem.selectedImage = UIImage(named: "tab_favorite_selected")

        let nvcMy = UINavigationController(rootViewController: viewMy)
        nvcMy.tabBarItem = UITabBarItem(title: mainTabs[4], image: UIImage(named:"tab_my"), tag:4)
        nvcMy.tabBarItem.selectedImage = UIImage(named: "tab_my_selected")

        self.viewControllers = [nvcConversation, nvcContact, nvcFile, nvcFavorite, nvcMy]

        UITabBar.appearance().barTintColor = ColorMainTabBackground

        //set bar item font
        let barItemFont = FontMainTab
        for nvcController in self.viewControllers!{
            nvcController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)
        }

        //default select conversation index 0
        self.selectedIndex = 0
但是觉得不够标准。
想要去搞懂:
如何写tab才是标准的写法。
[折腾过程]
1.搜:
swift add tab
参考:
搜:
swift add tab bar item programmatically
swift add tab view programmatically
参考:
UITabBarController详解 – Carl_Huang的个人页面 – 开源中国社区

然后用下面代码:
import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let mainTabsTitleArr = ["消息", "通讯录", "文件", "收藏", "我"]

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        //application.statusBarHidden = true;
        statusBarFrame = application.statusBarFrame
        //print("statusBarFrame=\(statusBarFrame)")

        //init a view controller
        let viewConversation    = ConversationViewController()
        //init a UITabBarItem, set to view controller's tabBarItem
        viewConversation.tabBarItem = UITabBarItem(title: mainTabsTitleArr[0], image: UIImage(named:"tab_conversation"), tag:0)
        //set selected image
        viewConversation.tabBarItem.selectedImage = UIImage(named: "tab_conversation_selected")

        let viewContact         = ContactViewController()
        viewContact.tabBarItem = UITabBarItem(title: mainTabsTitleArr[1], image: UIImage(named:"tab_contact"), tag:1)
        viewContact.tabBarItem.selectedImage = UIImage(named: "tab_contact_selected")

        let viewFile            = FileViewController()
        viewFile.tabBarItem = UITabBarItem(title: mainTabsTitleArr[2], image: UIImage(named:"tab_file"), tag:2)
        viewFile.tabBarItem.selectedImage = UIImage(named: "tab_file_selected")

        let viewFavorite        = FavoriteViewController()
        viewFavorite.tabBarItem = UITabBarItem(title: mainTabsTitleArr[3], image: UIImage(named:"tab_favorite"), tag:3)
        viewFavorite.tabBarItem.selectedImage = UIImage(named: "tab_favorite_selected")

        let viewMy              = MyViewController()
        viewMy.tabBarItem = UITabBarItem(title: mainTabsTitleArr[4], image: UIImage(named:"tab_my"), tag:4)
        viewMy.tabBarItem.selectedImage = UIImage(named: "tab_my_selected")

        //init a tab bar controller
        let mainTabBarController = UITabBarController()
        //add view controller array to viewControllers of tab bar controller
        mainTabBarController.viewControllers = [viewConversation, viewContact, viewFile, viewFavorite, viewMy]

        //set tab bar controller as window root view controller
        window?.rootViewController = mainTabBarController

        return true
    }
效果是:
可以正确显示tab的
不过,对于页面的布局来说,顶部的statusBar都会被当前的view占用了:
parent view top override status bar
然后进入第一个页面的字页面后,顶部的状态栏依然被覆盖了,底部也没了对应的tab栏了.
然后如果把第一个viewcontroller换成是:UINavigationController
        //init a view controller
        let viewConversation    = ConversationViewController()
//        //init a UITabBarItem, set to view controller's tabBarItem
//        viewConversation.tabBarItem = UITabBarItem(title: mainTabsTitleArr[0], image: UIImage(named:"tab_conversation"), tag:0)
//        //set selected image
//        viewConversation.tabBarItem.selectedImage = UIImage(named: "tab_conversation_selected")
        let nvcConversation = UINavigationController(rootViewController: viewConversation)
        nvcConversation.tabBarItem = UITabBarItem(title: mainTabsTitleArr[0], image: UIImage(named:"tab_conversation"), tag:0)
        nvcConversation.tabBarItem.selectedImage = UIImage(named: "tab_conversation_selected")

        //init a tab bar controller
        let mainTabBarController = UITabBarController()
        //add view controller array to viewControllers of tab bar controller
        //mainTabBarController.viewControllers = [viewConversation, viewContact, viewFile, viewFavorite, viewMy]
        mainTabBarController.viewControllers = [nvcConversation, viewContact, viewFile, viewFavorite, viewMy]
那么效果就变了:
加了navigation的viewcontroller后,视图中顶部就自动加上了导航栏:
current parent page show navi bar
并且,进入子页面后,顶部自动出现导航栏点返回Back按钮,同时,底部也会保留对应的tab

转载请注明:在路上 » [已解决]搞懂swift写tab的标准写法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.199 seconds, using 22.06MB memory