2015-03-23 18 views
126

Làm cách nào để ẩn thanh điều hướng từ ViewController đầu tiên hoặc một ViewController cụ thể nhanh chóng?Làm cách nào để ẩn thanh điều hướng từ ViewController đầu tiên trong Swift?

tôi đã sử dụng đoạn mã sau vào viewDidLoad():

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.navigationController?.isNavigationBarHidden = true 
} 

và cũng trên viewWillAppear:

override func viewWillAppear(animated: Bool) { 
    self.navigationController?.isNavigationBarHidden = true 
} 

Cả hai phương pháp ẩn bộ điều khiển chuyển hướng từ khắp nơi ViewControllers.

+0

bạn cần phải quản lý nó bằng tay cho tất cả viewcontrollers .. bạn không thể làm điều đó cho bất kỳ một .. – itsji10dra

Trả lời

94

Nếu bạn biết rằng tất cả các quan điểm khác nên có thanh có thể nhìn thấy, bạn có thể sử dụng viewWillDisappear để thiết lập nó để có thể nhìn thấy lần nữa.

+1

Câu trả lời này là hiệu quả hơn . Hãy suy nghĩ về mã lặp đi lặp lại với mỗi ViewController mới mà bạn thêm vào. http://stackoverflow.com/a/39679506/5079380 –

61

Bạn có thể thôi ẩn navigationController trong viewWillDisappear

override func viewWillDisappear(animated: Bool) 
{ 
    super.viewWillDisappear(animated) 
    self.navigationController?.isNavigationBarHidden = false 
} 

Swift 3

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 

    self.navigationController?.setNavigationBarHidden(false, animated: animated) 
} 
152

Swift 3

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    // Hide the navigation bar on the this view controller 
    self.navigationController?.setNavigationBarHidden(true, animated: animated) 
} 

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 

    // Show the navigation bar on other view controllers 
    self.navigationController?.setNavigationBarHidden(false, animated: animated) 
} 
+2

Với ghi đè không quên gọi các phương thức siêu: super.viewWillAppear (hoạt ảnh) và super.viewWillDisappear (hoạt ảnh) – NielsKoole

+0

Bất kỳ vấn đề nào nếu tôi không gọi super.viewWillAppear. ..... hoặc phương pháp siêu khác ??? – Athul

+0

đoạn mã tốt - cảm ơn bạn! nó làm việc tốt cho tôi !! – nurxyz

6

Trong Swift 3, bạn có thể sử dụng isNavigationBarHidden tài sản cũng để hiển thị hoặc ẩn thanh điều hướng

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    // Hide the navigation bar for current view controller 
    self.navigationController?.isNavigationBarHidden = true; 
} 

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 
    // Show the navigation bar on other view controllers 
    self.navigationController?.isNavigationBarHidden = false; 
} 
0

Bạn có thể làm điều đó từ bộ điều khiển cửa sổ (Swift3)

class WindowController: NSWindowController { 

    override func windowDidLoad() { 
     super.windowDidLoad() 

     window?.titleVisibility = .hidden 
    } 
} 
+3

Bộ điều khiển cửa sổ là gì? – MBH

7

Bạn cũng có thể tạo tiện ích mở rộng cho việc này để bạn có thể sử dụng lại tiện ích mà không cần triển khai điều này một lần nữa và một lần nữa trong mỗi bộ điều khiển xem.

import UIKit 

extension UIViewController { 
    func hideNavigationBar(){ 
     // Hide the navigation bar on the this view controller 
     self.navigationController?.setNavigationBarHidden(true, animated: true) 

    } 

    func showNavigationBar() { 
     // Show the navigation bar on other view controllers 
     self.navigationController?.setNavigationBarHidden(false, animated: true) 
    } 

} 

Vì vậy, bạn có thể sử dụng các phương pháp khuyến nông như sau

override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     hideNavigationBar() 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 
     showNavigationBar() 
    } 
+2

Không thực sự đáng để mở rộng, phải không? :) –

+1

Phụ thuộc vào số lượt xem bạn đang ẩn/hiển thị thanh điều hướng. Tôi cảm thấy như hầu hết các trường hợp, bạn chỉ ẩn cái đầu tiên nhưng nếu bạn làm điều đó thì phần mở rộng rất đẹp. – jnwagstaff

Các vấn đề liên quan