Generate Navigation Bar View by Code (Not using Storyboard) - ios

I am trying to code without using Storyboard or Interface Builder in my project. So, in the need of create a side menu for my app, I want to create a Navigation Bar to give the chance to open the menu by tapping on the left button of my navigation Bar.
This is what I have tried with no success:
override func viewDidLoad() {
super.viewDidLoad()
centerViewController = ViewController()
centerViewController.delegate = self
centerNavigationController = UINavigationController(rootViewController: centerViewController)
let menuButton: UIBarButtonItem = UIBarButtonItem(title: "TMDB", style: .plain, target: centerViewController, action: Selector(("toggleLeftButton")))
centerNavigationController.navigationItem.leftBarButtonItem = menuButton
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMove(toParentViewController: self)
}
My Navigation Bar is not showing any button nor title.

I know that is no longer necessary but still. His lessons are helpfull https://www.youtube.com/watch?v=zS-CCd4xmRY

Solved
The problem was where I was writting the code. It must be instantiated inside the viewController where the NavigationController belongs.
I mean, inside the centerViewController not in his parent as I was doing.
This lines of code:
let menuButton: UIBarButtonItem = UIBarButtonItem(title: "TMDB", style: .plain, target: self, action: Selector(("toggleLeftButton")))
self.navigationItem.leftBarButtonItem = menuButton
Inside the controller ViewController.
EDIT
Reference:
Adding UIBarButtonItem to UINav..Controller

Related

Why does setting a custom backBarButtonItem for a UINavigationItem result in double back buttons?

I have a very simple setup. A UINavigationController with a root UIViewController that modifies its navigation item with a custom back button item on viewDidLoad.
let backButton = UIBarButtonItem(image: backArrowImage,
style: .plain,
target: nil,
action: nil)
navigationItem.backBarButtonItem = backButton
I'm expecting this to completely replace the system back button with title and the default back arrow icon.
However when I push a new view controller on the stack, the navigation bar draws both the new custom back icon and the system back icon.
This is what I'm seeing:
This is what I would expect it to look like:
You can hide the back button
navigationItem.hidesBackButton = true
and use leftBarButtonItem for custom UIBarButtonItem
UPD
import UIKit
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
let backItem = UIBarButtonItem(image: backArrowImage, style: .plain, target: self, action: #selector(backButtonPressed))
navigationItem.leftBarButtonItem = backItem
}
#objc func backButtonPressed() {
navigationController?.popViewController(animated: true)
}
}
let backBarButtonItem: UIBarButtonItem = .init(
image: UIImage(systemName: "chevron.backward"),
style: .plain,
target: target,
action: action
)
navigationBar.topItem?.backBarButtonItem = backBarButtonItem
navigationBar.backIndicatorImage = UIImage()
navigationBar.backIndicatorTransitionMaskImage = UIImage()
This works for me, to setup custom "<" and hide the default one and still keep the backBarButtonItem behaviours
The solution was to set global UINavigationBar appearance.
Apparently this has to be done at app launch.
UINavigationBar.appearance().backIndicatorImage = backArrowImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backArrowImage
With this approach we can preserve title animations and general back button behavior that would not be preserved if supplementing the back button with the leftBarButtonItem.

Change Right Bar Button Items on Current Screen

I have spent all day trying to figure this out and I'm beyond frustrated.
I have a navigation stack and on each screen, based on the content of the screen, I need to hide/show some of the right bar button items. But no matter what I do, the items are changing on the previous screen on the stack, not on the current screen.
My view controllers are all pushed onto a navigation controller. And each view controller can instantiate another view controller and push onto the navigation stack.
let vc = UnifiedArticleViewController()
navigationController?.pushViewController(vc, animated: true)
I have tried the following:
navigationController?.navigationBar.topItem?.rightBarButtonItems = [arrayOfBarButtonItems]
and:
navigationItem.rightBarButtonItems = [arrayOfBarButtonItems]
as well as a variety of other suggestions I've gotten from various stack overflow suggestions.
I have been able to change the title of screens using:
navigationController?.navigationBar.topItem?.title = "New Title Here"
and that works perfectly. What am I doing wrong?
Set the leftBarButtonItems or rightBarButtonItems from the controller you're wanting to set the button(s) on that is in the navigation controller's view stack.
class ViewController: UIViewController {
override func viewDidLoad() {
let leftButton = UIBarButtonItem(title: "Left", style: .done, target: self, action: #selector(leftButtonPressed(_:)))
self.navigationItem.leftBarButtonItems = [leftButton]
let rightButton = UIBarButtonItem(title: "Right", style: .done, target: self, action: #selector(rightButtonPressed(_:)))
self.navigationItem.rightBarButtonItems = [rightButton]
}
#objc func leftButtonPressed(_ sender: UIBarButtonItem) {
}
#objc func rightButtonPressed(_ sender: UIBarButtonItem) {
}
}

Set UIBarButtonItem target programmatically to previous view Controller

I have two UIViewController (I did not use UINavigationController) which are named as ParentViewController and ChildViewController.
However, I can't add UINavigationBar using storyboard for the child view controller, so I add the UINavigationBar with UIBarButtonItem inside it programmatically.
I've successfully add the navigation bar and the bar button item to the child view controller. The problem I got that I can't set the target for the UIBarButtonItem, so when it pressed, the Parent view controller will show up.
This is the code I use, but I didn't sure where to put them
let navigationBar : UINavigationBar = { //Label to display the text
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
navBar.translatesAutoresizingMaskIntoConstraints = false
let navItem = UINavigationItem(title: "SomeTitle");
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: "selector");
let backItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(backAction(_:)));
//let backItem: UIBarButtonItem = backsItem
navItem.rightBarButtonItem = doneItem;
navItem.leftBarButtonItem = backItem;
navBar.setItems([navItem], animated: false);
return navBar;
}();
#IBAction func backAction(_ sender: Any?) {
self.navigationController?.popViewController(animated: true)
}
And then I add the navigationBar to the subView in Child View Controller viewDidLoad()
For your information, I did not do anything in the parent view controller. It just the segue I created on storyboard to show the child view controller when pressed.
please kindly help me...
I already found the solution. Instead of using self.navigationController to show the previous controller, I used the general present command to show the view controller.
This is my code, and it solved all of my problems.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ParentViewController")
self.present(vc!, animated:true, completion:nil)
I hope it will help the others who facing a problems like me.

Swift: Can't add custom button to navigation controller

I'm trying to create a custom navigation bar, and I'm having difficulty modifying different parts of the navigation bar. I can change the color of the background, but I can't seem to add buttons or change the title.
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// changing the background color works
self.navigationBar.barTintColor = UIColor.purpleColor()
// none of this works
let leftButton = UIBarButtonItem(title: "Info", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(openInfo))
self.navigationItem.leftBarButtonItem = leftButton
self.navigationItem.title = "MYTITLE"
}
}
I'm not sure if the fact that I'm trying to integrate this NavigationController with a TabBarController is affecting the way the view loads, but this custom NavigationController is being subclassed by each tab in the TabBarController.
According to UINavigationItem class reference, each view controller has its own UINavigationItem instance. "The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content", which means its UIViewController's responsibility to create the navigation item content such as left bar item or title.
I can understand that you want to provide the same appearance of navigation bar throughout the app. But why do you want to set the same title for all view controllers? However, if same title and same left bar item for all view controllers is what you need. Here are two solutions:
1). Make an extension to UIViewController:
extension UIViewController {
func customAppearance() {
let leftButton = UIBarButtonItem(title: "Info", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(openInfo))
self.navigationItem.leftBarButtonItem = leftButton
self.navigationItem.title = "MYTITLE"
}
func openInfo() {
// do what you want
}
}
And then whenever you need a customised navigation bar for a view controller, you call this customAppearance function:
let vc = YourViewController()
vc.customAppearance()
2). Subclass the UIViewController:
class CustomViewController: UIViewController {
override func viewDidLoad() {
let leftButton = UIBarButtonItem(title: "Info", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(openInfo))
self.navigationItem.leftBarButtonItem = leftButton
self.navigationItem.title = "MYTITLE"
}
func openInfo() {
}
}
And your every other view controllers subclass this CustomViewController.
For customzing UINavigationBar's appearance, you can set it like:
UINavigationBar.appearance().barTintColor = UIColor.purpleColor()

Button in Navigation Bar in Tab Bar UIViewController not showing

I can't seem to get any buttons to appear in my navigation bar.
Flow: Navigation Controller -> Tab bar Controller -> ViewController (want to put button here)
I've tried adding it programmatically inside of ViewDidLoad:
let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.blackColor()
let leftButton = UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
I've also tried adding it using the storyboard but it doesnt show during runtime. Any idea why I can't get a button to appear in the navigation bar
Try it like this instead:
self.tabBarController?.navigationItem.leftBarButtonItems = [leftButton]
views from storyboard or xib load after viewWillAppear method call. and viewDidLoad method call before viewWillAppear.
so navigation bar is nil during viewDidLoad method so they are not appear.so write you code in viewdidappear method and your buttons will be appeared.

Resources