你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift iOS : UITabBarController

Swift iOS : UITabBarController

編輯:IOS開發綜合

類UITabBarController是一個特殊的UIViewController,它可以包含多個UIViewController,並且在頁面底部顯示一個Tabbar作為UIViewController的切換顯示開關。

如下案例,展示了包含兩個UIViewController的UITabBarController,可以通過底部的Tabbar來切換顯示:

    import UIKit
    class Page1: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let cc = UILabel(frame: CGRect(x: 10,y: 50,width: 200,height: 50))
            cc.text = "Page #1"
            cc.textColor = UIColor.black
            self.view.addSubview(cc)
        }
    }
    class Page2: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let cc = UILabel(frame: CGRect(x: 10,y: 50,width: 200,height: 50))
            cc.text = "Page #2"
            cc.textColor = UIColor.black
            self.view.addSubview(cc)
        }
    }
    class Tabbar: UITabBarController, UITabBarControllerDelegate {
        override func viewDidLoad() {
            super.viewDidLoad()
            delegate = self
        }
        
        override func viewWillAppear(_ animated: Bool) {
            viewControllers = [Page1(),Page2()]
            let r = UIImage.imageWithColor(UIColor.black)
            viewControllers![0].tabBarItem = UITabBarItem(title: "Page 1",
                            image:r,
                tag:0)
            viewControllers![1].tabBarItem = UITabBarItem(title: "Page 2",
                image: r,
                 tag:1)
    
        }
        
        //Delegate methods
        func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            print("did select viewController: \(viewController.tabBarItem.tag)")
        }
    }
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var Window: UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            self.Window = UIWindow(frame: UIScreen.main.bounds)
            self.window!.rootViewController = Tabbar()
            self.window!.rootViewController!.view.backgroundColor = UIColor.white
            self.window?.makeKeyAndVisible()
            return true
        }
    }
    // same code
    extension UIImage {
        class func imageWithColor(_ color: UIColor) -> UIImage {
            let rect = CGRect(x: 0.0, y: 0.0, width: 10.0,height: 10.0 )
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
            
            context?.setFillColor(color.cgColor)
            context?.fill(rect)
            
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            return image!
        }
    }
    //let r = UIImage.imageWithColor(UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal)
    //let b = UIImage.imageWithColor(UIColor.blueColor()).imageWithRenderingMode(.AlwaysOriginal)

可以點擊屏幕顯示的下方按鈕切換視圖控制器。

本案例中,共有三個需要關注的類:

Page1,Page2。都是UIViewController的子類,內含一個UILabel區別顯示為Page #1,Page #2

Tabbar 是UITabBarController的子類,實現協議UITabBarControllerDelegate,並在viewDidLoad時設置委托到自身;屬性viewControllers是一個數組,用於裝入多個UIViewController的實例,當執行:

func viewWillAppear(_ animated: Bool)

初始化此數組,創建需要選擇的UIViewController實例進來。

Tabbar就是一個UIViewController的子類,因此可以把它設置到self.window!.rootViewController上,從而成為window的rootViewController。

以上就是對Swift IOS : UITabBarController的相關介紹,希望對您學習IOS有所幫助,感謝您關注本站!

【Swift iOS : UITabBarController】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved