没有参考资料时,如何使用/编辑导航栏功能?

时间:2019-12-03 07:55:08

标签: ios swift

我目前正在学习iOS开发,即使没有IBOutlet,我也对如何编辑/使用导航栏感到困惑?

代码:

class ChatViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var messageTextfield: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "⚡️FlashChat" //edited the title of the navigation bar for this view
        navigationItem.hidesBackButton = true //hid a button

    }

    @IBAction func sendPressed(_ sender: UIButton) {
    }

    @IBAction func logoutPressed(_ sender: UIBarButtonItem) {
        do {
            try Auth.auth().signOut()
            navigationController?.popToRootViewController(animated: true)
        } catch let signOutError as NSError {
            print ("Error signing out: %@", signOutError)
        }

    }

}

2 个答案:

答案 0 :(得分:0)

Appdelegate.swift 文件中的setupNavBarAttributes()调用func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool函数,并观察更改。

它将自定义应用程序中每个 UIViewController UINavigationBar

func setupNavBarAttributes() {

        UINavigationBar.appearance().barStyle = .default
        UINavigationBar.appearance().tintColor = .cyan  // change color of the buttons of navigationbar
        UINavigationBar.appearance().barTintColor = .blue  // change front color of navigationbar
        UINavigationBar.appearance().backgroundColor = .clear
        UINavigationBar.appearance().isTranslucent = true
        UINavigationBar.appearance().shadowImage = UIImage() // make disappear the bottom shadow of navigationbar
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default) // make background to a blank/empty image

        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 19, weight: .bold)]  // change the font color of navigation title

    }

答案 1 :(得分:0)

导航栏不是UIViewController的一部分,它们实际上是父UINavigationController的一部分,父UINavigationController封装了UIViewController。因此,当您访问navigationItem时,实际上就是在访问导航控制器的栏。

详细了解容器视图控制器here