Custom navigation bar for pushed UIViewController - ios

How do I set a title and buttons on the navigation bar of a pushed view controller if I push it onto navigationController with navigationController.pushViewController(controller:animated:completion:), whilst keeping the "Back" button?
Thank you in advance!

For anyone wondering how to do this, just override your pushed UIViewController's navigationItem property directly. My mistake was that I was trying to accomplish this with self.navigationController?.navigationItem

Here is the code, that you should write in order to set the navigation title as well as left button
Code :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//For Setting Title
self.title = "New Title";
//For setting button in place of back button
let leftItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "leftButtonClicked");
self.navigationItem.leftBarButtonItem = leftItem;
}
Happy coding ...

Related

Unable to add barbuttonitem to navigation controller

I would like to integrate both navigation controller and tab bar controller in my project.But I am unable to add right barbutton to the navigation controller.
I have attached the screenshot of the storyboard
What I have done is I have added navigation controller to login screen and this time I am able to add barbuttonitem both by adding code as well as by dragging barbuttonitem to navigation controller.
let addBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
self.navigationItem.rightBarButtonItem = addBtn
Problem I am facing is after adding Tab bar controller I am unable to add rightbarbutton both by code as well as by dragging to the navigation controller. please help me.
When a ViewController is embedded in a NavigationController you use
self.navigationItem.rightBarButtonItem = addBtn
In your project Detail Screen isn't embedded in NavigationController directly. Detail Screen is embedded in TabBarController, TabBarController is embedded in NavigationController. So you should use
self.tabBarController?.navigationItem.rightBarButtonItem = addBtn
But this addBtn will be visible in all view controllers which are embedded in the TabBarController.
If you want to add the rightBarButton for only one viewcontroller, then embed the Detail Screen in a new NavigationController. Then you can add rightBarButton using
self.navigationItem.rightBarButtonItem = addBtn
You should be sure parent returns the top child controller of UINavigationController. In my case
parent?.parent?.navigationItem.right...
did the trick.
If you reuse the controller -as embedded or not- which you want add items to navigationItem, following example will work. However some logical changes may be needed.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard parent is UINavigationController else {
parent?.parent?.navigationItem.rightBarButtonItem = UIBarButtonItem()
return
}
navigationItem.rightBarButtonItem = UIBarButtonItem()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
guard parent is UINavigationController else {
parent?.parent?.navigationItem.rightBarButtonItem = nil
return
}
navigationItem.rightBarButtonItem = nil
}

Create big navigation bars with title in Xcode

I'm new into iOS development and I'd like to know how to create navigation bars like this one:
I've been creating this manually for some time, but have recently discovered there was a way to accomplish this by using Navigation Items and Bars.
I've also tried looking up on the internet on how to do this, but haven't found anything.
Sorry if it is a stupid question and something really easy to do.
Thanks
UINavigationController‘s UINavigationBar has the prefersLargeTitles property. Set it to true to enable large title
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Home"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refreshBtnAction(_:)))
}
#objc func refreshBtnAction(_ sender: Any) {
}
In Xcode go to Interface builder, this a tool which display content
of a storyboard or a xib files.
Select a navigation bar
In the attributes inspector which is on the left, you will find a property "Prefers Large Titles". Check it.

Button in navigation bar

How do I add a button to navigation bar that appears only in a particular view controller? And also how to add different buttons to different view controller that is embedded in navigation controller.
You don't have to add buttons to navigation bar, rather you could add bar button items. If you want you can customise bar button item to meet your requirements like setting a custom image as suggested by #Pushpendra.
Programatically:
Your View Controller is responsible for providing bar button item(s) for it's navigation bar. Create bar button items inside each of your view controller's viewDidLoad() or in init() if you have overridden it.
Storyboards:
It's pretty handy to drag n drop bar button items from storyboard editor and have the outlets setup so that you can change the behaviour later.
If you are precisely looking to control the behaviour, then you can add or remove a bar button as and when needed depending on your conditional requirements.
class FooViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/// Assuming this view controller presented/pushed with navigation controller
let buttonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(FooViewController.buttonItemAction(sender:)))
self.navigationItem.leftBarButtonItem = buttonItem
}
#IBAction func buttonItemAction(sender: UIBarButtonItem) {
/// some action
}
}
class BarViewController: UIViewController {
var buttonItem: UIBarButtonItem?
var showButtonItem = false
override func viewDidLoad() {
super.viewDidLoad()
/// Assuming this view controller presented/pushed with navigation controller
buttonItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(BarViewController.buttonItemAction(sender:)))
self.navigationItem.leftBarButtonItem = buttonItem
}
#IBAction func buttonItemAction(sender: UIBarButtonItem) {
/// some action
}
#IBAction func toggleButtonItem(sender: UIButton) {
self.navigationItem.rightBarButtonItem = (self.showButtonItem == true) ? self.buttonItem : nil
}
}
If this is what you are not looking for then update your question along with the code that you have tried.
try this :-
extension UIViewController {
func addButton(img:String){
let imgIcon = UIImage(named: img)
let btn = UIBarButtonItem(image: imgIcon, style: .plain, target:
self, action: #selector(self.btnAction))
// if you want to set right button then add right bar button item
self.navigationItem.leftBarButtonItem = btn
}
func btnAction(){
}
func removeButton() {
self.navigationItem.leftBarButtonItem = nil
}
}
when you want to add button
in viewDidLoad :-
self.addButton(img:"imageName")
also if you want to perform separate action on all view controller then
call this on controller
override func btnAction() {
}
to remove :-
self.removeButton()
if you want to add different button on different controller add the button in viewDidLoad and remove in viewWillDisappear
Drag a bar button item in your navigation bar for the specific view controller in your storyboard
The best solution is
Create Viewcontroller inherit from UIViewController or UITableViewController and write a method for setupNavigationButton or buttonText or
(Better to add an action method on a super controller also)
eg:
-(void) setupNavigationButton {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"button1" style:UIBarButtonItemStylePlain target:self action:#selector(buttonActin:)];
}
and call this method on viewDidLoad on the created controller
Next when you create your own controller add this viewcontroler as your parent view controller. So it will automatically add that view.
(You want to write an action on your controller)
Using above method, it saves only one line. But you can save more lines. Let's assume you want to change only text and action on that button.
If it is only you need to change text, write a method to return button text
(Following line for your baseviewcontroller)
-(NSString *) rightButtonText {
return #"";
}
and add following codes for baseviewcontroller
-(void)setNavigationControler {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:[self rightButtonText] style:UIBarButtonItemStylePlain target:self action:#selector(buttonActin:)];
}
and call it on baseviewcontroller
Now your every view controller, you need to override rightButtonText and create selector method and do that action.
Assume you don't need to add that button for some viewcontroler. Then you have a few options
1) Remove baseviewcotroler inheritance from your viewcontroller
2) After call supper viewDidload add following code
self.navigationItem.rightBarButtonItem =
3) Override setNavigationControler method on your controller with the blank method body.
-(void)setNavigationControler {}

Swift how to preserve navigation bar button items after push

In my root view controller I set an array of bar button items with images and assign them to the right bar button.
When I push the next view controller my navigation bar resets and only displays a back button.
Any way to preserve the navigation bar as it was set on the root view controller so it will display on all pages?
As user1046037 has said you can set the item buttons while you are preparing the segue.
Example:
let helpViewController = HelpViewController(nibName: "HelpViewController", bundle: nil)
let someLeftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: "someAction")
helpViewController.navigationItem.leftBarButtonItem = someLeftButton
helpViewController.navigationItem.leftItemsSupplementBackButton = true
navigationController?.pushViewController(helpViewController, animated: true)
This one is to preserve the left button item and the back one.
helpViewController.navigationItem.leftItemsSupplementBackButton = true
If you are going to use the same button in several Viewcontrollers you can create a BaseViewController setting up the button and his behaviors.
class AHBaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
// Configure Common status bar if you want too.
}
func configureNavigationBar() {
let someLeftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: "someAction")
navigationItem.leftBarButtonItem = someLeftButton
navigationItem.leftItemsSupplementBackButton = true
}
}
Then just inherit it, in the viewControllers that you want to show the button(s).
class HelpViewController: AHBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
When you segue between view controllers using Push, you're going to get the default "horizontal slide" navigation animation which. Try "present Modally" or "present as popover".

iOS set BackButton in the current ViewController

How can I change the Back-Button of the currently displayed ViewController?
Configuring the BackButtonItem effects the BackButton of the next ViewController, instead of the currently shown.
e.g.: I have two ViewControllers VCA and VCB. I navigate from VCA to VCB and want to style the BackButton thats shown on VCB. I want to hide the title and set custom icons for normal and highlighted state.
public class VCB : ViewControllerBase
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
var btn = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
// TODO: set the Icon
NavigationItem.BackBarButtonItem = btn;
}
}
Do I have to implement my own BackButton and set it as LeftBarButton, or can I achieve this somehow with the standard BackButton .
You can try this in your ViewDidLoad method:
UIBarButtonItem backButton = new UIBarButtonItem("< Back",UIBarButtonItemStyle.Plain, null);
this.NavigationItem.SetLeftBarButtonItem(backButton, false);
if the viewController already has a BarButtonItem in the View,you should be able to use this.NavigationItem.BackBarButtonItem and change its properties.

Resources