UINavigationBar와 그 안의 Item들을 코드로 어떻게 작성하는지 알아보겠습니다.
let navigationBar : UINavigationBar = {
let navigationBar = UINavigationBar()
navigationBar.translatesAutoresizingMaskIntoConstraints = false
return navigationBar
}
rightBarbutton, leftBarButton, title 등은 UINavigationItem에 속해있기 때문에 이들을 넣어 줄 UINavigationItem을 만들어주어야합니다
override func viewDidLoad() {
let navItem = UINavigationItem(title: "알림 추가")
}
override func viewDidLoad() {
let leftButton = UIBarButtonItem(title: "취소", style: .plain, target: self, action: #selector(tapDismissButton))
let rightButton = UIBarButtonItem(title: "저장", style: .plain, target: self, action: #selector(tapSaveButton))
navItem.rightBarButtonItem = rightButton
navItem.leftBarButtonItem = leftButton
}
override func viewDidLoad() {
navigationBar.setItem([navItem], animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.addSubview(navigationBar)
let safeArea = self.view.safeAreaLayoutGuide
navigationBar.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
navigationBar.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
navigationBar.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
let navItem = UINavigationItem(title: "알람 추가")
let leftButton = UIBarButtonItem(title: "취소", style: .plain, target: self, action: #selector(tapDismissButton))
let rightButton = UIBarButtonItem(title: "저장", style: .plain, target: self, action: #selector(tapSaveButton))
navItem.rightBarButtonItem = rightButton
navItem.leftBarButtonItem = leftButton
navigationBar.setItems([navItem], animated: true)
}