Swift_UI:(四)、导航控制器、TabbarController、页面调转

许安邦
2023-12-01

AppDelegate.swift文件中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        let mainVC = ViewController()

        let mainNavigationC = UINavigationController(rootViewController: mainVC)

        mainNavigationC.tabBarItem.title = "首页"

        mainNavigationC.tabBarItem.image = UIImage(named:"首页")


        let secondVC = SecondViewController()

        let seNavigationC = UINavigationController(rootViewController: secondVC)

        seNavigationC.tabBarItem.title = "发现"

        seNavigationC.tabBarItem.image = UIImage(named:"发现")

        

        let tabC = UITabBarController()

        tabC.viewControllers = [seNavigationC,mainNavigationC]

        self.window?.rootViewController = tabC

        

        return true

    }


ViewController.swift文件中:

import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        self.title = "导航栏"

//        设置导航栏颜色

        self.navigationController?.navigationBar.barTintColor = UIColor.green

//        添加左按钮

        let barBttonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target:self, action: #selector(click))

//        导航栏右按钮

        let rightBarBttonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target:self, action: #selector(rightClick))


        self.navigationItem.leftBarButtonItem = barBttonItem

        self.navigationItem.rightBarButtonItem = rightBarBttonItem


        // Do any additional setup after loading the view, typically from a nib.

    }

    

    func click()  {

        print("点击导航栏左侧按钮")

    }

    

    func rightClick() {

        print("点击导航栏右侧按钮")

    }

    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        

        self.navigationController?.pushViewController(MainViewController(), animated: true)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}


MainViewController.swift文件中:

override func viewDidLoad() {

    super.viewDidLoad()

    self.view.backgroundColor = UIColor.yellow

        // Do any additional setup after loading the view.

    }


SecondViewController.swift文件中:

import UIKit


class SecondViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        // Do any additional setup after loading the view.

    }

    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.navigationController?.pushViewController(ThridViewController(), animated: true)

        

        //作业:仿照淘宝框架,要求设置导航栏和tabbar样式一致,使用siwift语言

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}


ThridViewController.swift文件中:

import UIKit


class ThridViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.gray

        // Do any additional setup after loading the view.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}




 类似资料: