UINavigationController
Embed in 으로 navigation controller 를 VC에 붙이면 자동으로 NavigationBar 가 붙는다. 이 때 조작방법
기본 사용법
// Push View Controller Onto Navigation Stack navigationController.pushViewController(viewController, animated: true)
// Pop View Controller From Navigation Stack navigationController.popViewController(animated: true)
root view controller 구하기
// Access View Controllers of Navigation Controller
navigationController.viewControllers
// Access Root View Controller of Navigation Controller
navigationController.viewControllers.first
NavigationBar 감추기
navigationController?.isNavigationBarHidden = true
// or
navigationController.setNavigationBarHidden(false, animated: true)
UIViewController 전환하기
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SB_PLAN") as! PlanVC
self.navigationController?.pushViewController(vc, animated: true)
pop gesture 막기
// 아래의 코드를 트리거하면 Pop 제스처를 비활성화할 수 있습니다
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
자동으로 붙는 < 백 버튼 color
self.navigationController?.navigationBar.tintColor = .systemPink
백 버튼 글자 “뒤로” 감추기 // “뒤로” 감추기 , 두번째 VC에서 self.navigationController?.navigationBar.topItem?.title = “” 또는 self.navigationController?.navigationItem.backBarButtonItem
이렇게 backBarButonItem이라는 직관적인 프로퍼티가 있거든요.
let backBarButtonItem = UIBarButtonItem(title: “Zedd”, style: .plain, target: self, action: nil)
self.navigationItem.backBarButtonItem = backBarButtonItem
or let backBarButtonItem = UIBarButtonItem(title: “”, style: .plain, target: self, action: nil) backBarButtonItem.tintColor = .white self.navigationItem.backBarButtonItem = backBarButtonItem
back 버튼 자체 감추기 self.navigationItem.setHidesBackButton(true, animated: false)
혹은 대체하기 let bbiCancel = UIBarButtonItem(image: UIImage(named: “icon_menu”), style: .plain, target: self, action: #selector(bbiCancelTapped)) bbiCancel.tintColor = UIColor(hexString: “4C4E51”)!
let leftBarButtons: [UIBarButtonItem] = [bbiCancel] navigationItem.leftBarButtonItems = leftBarButtons
This code set’s the navigation bar’s back button as hidden.
self.navigationController?.navigationItem.hidesBackButton = true
This code set’s the navigation bar’s back button as nil
self.navigationItem.leftBarButtonItem = nil;
A combination of these to approaches would be a better solution and works even if you have set a custom navigation bar.
self.navigationItem.leftBarButtonItem = nil self.navigationItem.hidesBackButton = true You can also use
override func viewDidLoad() { super.viewDidLoad()
}
타이틀 설정
// ViewController 타이틀을 지정하면 NavBar 타이틀이 자동으로 표시된다.
self.title = "Title"
or
self.navigationItem.title = "Title"
//
viewController.title = "이것이 바로 타이틀입니다"
백버튼에도 자동으로 붙는다. 없애거나,
self.navigationController?.navigationBar.topItem?.title = ""
다음 뷰의 백 버튼 옆에 표시될 글자 설정
self.navigationController?.navigationBar.topItem?.title = "Din"
가운데 타이틀 설정하는 방법
// titleView로 사용할 Label을 생성
let label = UILabel(frame: customFrame)
label.text = "이것을 타이틀로 사용합니다"
// viewController의 titleView를 생성한 Label로 셋업
viewController.titleView = label
bar button item
// RightBarButtons에 추가할 UIBarButtonItem을 생성
let customButton = UIBarButtonItem(customView: customView)
// Container가 될 Array를 생성 (혹은 직접 지정하는 방법도 있습니다)
var rightBarButtons: [UIBarButtonItem] = []
// Array에 버튼 아이템을 추가
rightBarButtons.append(customButton)
// RightBarButtonItems 배열을 셋업
viewController.navigationItem.rightBarButtonItems = rightBarButtons
//LeftBarButtons에 추가할 UIBarButtonItem을 생성
let customButtonCopy = UIBarButtonItem(customView: customView)
// Container가 될 Array를 생성 (혹은 직접 지정하는 방법도 있습니다)
var leftBarButtons: [UIBarButtonItem] = []
// Array에 버튼 아이템을 추가
leftBarButtons.append(customButtonCopy)
// LeftBarButtonItems 배열을 셋업
viewController.navigationItem.leftBarButtonItems = leftBarButtons
root view 로 돌아가기
self.navigationController?.popToRootViewController(animated: true)
push 된 View 에서 barbuttonitem
let bbiCancel = UIBarButtonItem(image: UIImage(named: "add"), style: .plain, target: self, action: #selector(SetAlarmVC.bbiAddTapped))
let bbi = UIBarButtonItem(title:"달력", style: .plain, target: self, action: #selector(LBBITapped))
self.navigationItem.rightBarButtonItem = bbi
@objc func LBBITapped(_ sender: UIButton?) {
print("\(#function)")
//self.navigationController?.popViewController(animated: true)
}
색상 설정
TintColor : barbuttonitem 에 있는 글자나 이미지의 색상 설정, 자동으로 붙는 < Back 버튼 색상 설정, 가운데 타이틀은 안 바뀐다.
navigationController?.navigationBar.tintColor = UIColor.green
self.navigationController?.navigationBar.tintColor = .green
타이틀 색상 설정
self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.green]
// 네비게이션바의 배경색 설정
self.navigationController?.navigationBar.barTintColor = .gray
// 기본 반투명인데..불투명하게 하려면 아래처럼
self.navigationController?.navigationBar.isTranslucent = false
// backgroundColor 도 있는데, statusbar 아래부터 시작되는 사각형 영역이 background 임, isTranslucent = true 하면 보인다.
self.navigationController?.navigationBar.backgroundColor = .red
하단 경계선 없애기
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
//or
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.clipsToBounds = true
//or
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = false
// 이 방법은 아래 처리도 해줘야함
navigationBar에 barTintColor를 따로 줘야함.
View에 Navigation Bar 를 붙여서 처리할 때
@IBOutlet weak var NavBar: UINavigationBar!
let bbiSave = UIBarButtonItem(title: "추가", style: .plain, target: self, action: #selector(HomeVC.bbiAddItemTapped))
NavBar.topItem?.rightBarButtonItem = bbiSave
// 가운데 타이틀 설정
NavBar.topItem?.title = "app_lock"
// 왼쪽에 붙는 바버튼아이템 타이틀 설정
NavBar.topItem?.leftBarButtonItem?.title = "Back"
// 글자색 설정
NavBar.tintColor = UIColor.black
// 배경 투명하게
NavBar.isTranslucent = true
NavBar.backgroundColor = UIColor(hexString: "#626567")
NavBar.barTintColor = UIColor.darkGray
NavBar.setBackgroundImage(UIImage(), for: .default)
NavBar.shadowImage = UIImage()
@objc func bbiAddItemTapped(_ sender:UIButton?) {
let p = Double.random(in: 0.0...1.0)
let pin = Double.random(in: 0.0...1.0)
let pout = Double.random(in: 0.0...1.0)
let name = "name" + String(addCount)
print(" new name \(name)")
}
swipe back 막기
self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false;