Advertisement
Macjen

NavigationBar、TabBar Problem

Jul 21st, 2021
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.93 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Practice2:Navigation、TabBar
  4. //
  5. //  Created by Jhen Mu on 2021/7/19.
  6. //
  7.  
  8. import UIKit
  9.  
  10. class ViewController: UIViewController {
  11.     //MARK:- Properties
  12.    
  13.    
  14.  
  15.    
  16.     //MARK:- Methods
  17.     @objc func check(){
  18.         print("check button action")
  19.     }
  20.  
  21.     @objc func article(){
  22.         self.navigationController?.pushViewController(
  23.               ArticleViewController(), animated: true)
  24.     }
  25.    
  26.     @objc func setting(){
  27.         self.navigationController?.pushViewController(
  28.               SettingViewController(), animated: true)
  29.     }
  30.    
  31.    
  32.     //MARK:- Life Cycle
  33.     override func loadView() {
  34.         super.loadView()
  35.     }
  36.    
  37.     override func viewDidLoad() {
  38.         super.viewDidLoad()
  39.         theNavigation()
  40.         theTabBar()
  41.  
  42.     }
  43.     //MARK:-setNavigationBar
  44.     func theNavigation(){
  45.         self.view.backgroundColor = UIColor.white
  46.         self.title = "HomePage"
  47.         self.navigationController?.navigationBar.barTintColor = UIColor.white //設置導覽列的顏色
  48.         self.navigationController?.navigationBar.isTranslucent = false //
  49.        
  50.         let rightButton = UIBarButtonItem(title: "Setting",
  51.                                           style: .plain,
  52.                                           target: self,
  53.                                           action: #selector(ViewController.setting))
  54.         let leftButton = UIBarButtonItem(title: "Article",
  55.                                          style: .plain,
  56.                                          target: self,
  57.                                          action: #selector(ViewController.article))
  58.         self.navigationItem.rightBarButtonItem = rightButton
  59.         self.navigationItem.leftBarButtonItem = leftButton
  60.        
  61.     }
  62.    
  63.     //MARK:-setTabBar
  64.     func theTabBar(){
  65.         self.tabBarController?.tabBar.barTintColor = UIColor.white
  66.         self.tabBarController?.tabBar.isTranslucent = false
  67.         let theTabBar = UITabBarController()
  68.         theTabBar.tabBar.backgroundColor = UIColor.white
  69.        
  70.         let mainpage = ViewController()
  71.         let article = ArticleViewController()
  72.         let settings = SettingViewController()
  73.         let intro = IntroViewController()
  74.        
  75.         article.tabBarItem = UITabBarItem(title: "Article",
  76.                                           image:UIImage(named:"paid-articles"),
  77.                                           tag: 200)
  78.         settings.tabBarItem = UITabBarItem(title: "Setting",
  79.                                            image:UIImage(named:"settings"),
  80.                                            tag: 200)
  81.         intro.tabBarItem = UITabBarItem(title: "Introdce",
  82.                                         image:UIImage(named:"light-bulb"),
  83.                                         tag: 200)
  84.         theTabBar.viewControllers = [
  85.             mainpage,article,settings,intro
  86.         ]
  87.         theTabBar.selectedIndex = 2
  88.  
  89.     }
  90.    
  91.     //在viewDidLoad之後被執行,時間點在view被呈現之前,每次切換頁面時都會被執行
  92.     override func viewWillAppear(_ animated: Bool) {
  93.         super.viewWillAppear(animated)
  94.         print("viewWillAppear")
  95.     }
  96.    
  97.    
  98.     //時間點在viewWillAppear呈現之後被執行,時間在View呈現後,每次切換到這個頁面都會被執行
  99.     override func viewDidAppear(_ animated: Bool) {
  100.         print("viewDidAppear")
  101.     }
  102.    
  103.     //執行的時間點在 View 要結束前,每次要切換到別頁或是退出這個頁面時都會執行。
  104.     override func viewWillDisappear(_ animated: Bool) {
  105.         super.viewWillDisappear(animated)
  106.         print("viewWillDisappear")
  107.     }
  108.    
  109.     //執行的時間點在 View 完全結束後,每次要切換到別頁或是退出這個頁面時都會執行。
  110.     override func viewDidDisappear(_ animated: Bool) {
  111.         print("viewDidDisappear")
  112.     }
  113.    
  114.    
  115.    
  116.    
  117.  
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement