Invoking the real Back Button Icon - Swift - swift

The code below manages the Back button, but obviously puts the word Back up in lieu of the Icon:
self.navigationItem.hidesBackButton = false
let newBackButton = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "segueBack")
navigationItem.leftBarButtonItem = newBackButton
How do I get iOS the invoke the back button icon? In Cocoa there was a call along the lines of:
[backButton setImage:[UIImage imageNamed:BACK_BUTTON_DEFAULT_ICON] forState:UIControlStateNormal];

You have to create your own button object in order to set its title. You can initWithTitle or create it and then set the title afterward.
The backBarButtonItem only affects its child views that are pushed on top of it in the navigation stack. Depending on your design the root view can't be popped any further and can't have a back button. Though, you can affect the leftBarButtonItem.
Create a UIBarButtonItem instead of setting the title of the existing one.
For example:
let newBackButton = UIBarButtonItem (title: "Back", style: .Plain, target: self, action: "segueBack")
self.navigationItem.backBarButtonItem = newBackButton
Set it on the "parent" view controller, where the Back button will return to.

Related

How to set navigationBar back button image to default symbols?

there is one screen that i don't want to show back button symbols.
i create a empty image and change the navigation bar back button image.(code like following)
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "mainicon_gray-13")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "mainicon_gray-13")
navigationItem.backBarButtonItem = UIBarButtonItem(title: "demo", style: .plain, target: nil, action: nil)
screen like following Picture
But all of backBarButtonItem changed, i need to set backBarButtonItem to default symbols "<" when i back to the view.
Is there any way to set navigation bar back button image to default image?
i want the navigationBar like following picture
following is the way that i found without change back button settings to do same event.
use leftBarButtonItem and set popViewController to go back before screen.
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "test", style: .plain, target: self,action: #selector(backViewBtnFnc))
}
#objc func backViewBtnFnc(){
self.navigationController?.popViewController(animated: true)
}

Left bar button item on NavigationBar not show full text when i present the viewcontroller

Left bar button with image not show full text, when i present that viewcontroller.
i use below code for it
let leftButton: UIBarButtonItem = UIBarButtonItem(image: buttonImg, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.dismissVC))
leftButton.title = title
navigationItem.leftBarButtonItem = leftButton
And when i use backbarbuttonitem instead of leftBarButtonItem button not displayed.
please give need full suggestion

How do I get back button effect (<)

I am writing an app , where I want custom back effect of the default OS. Like the system which is giving with Settings ( <)
< Icon
Refer this image (I want the effect like Settings with the <)
I already have the functionality, I just want the look and feel.
Effect what I am getting.
I have tried the following
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
Also, I have tried adding it via Storyboard. How do I get it ?
Use the backBarButtonItem and you will get right effect.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
Also, you make sure
self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.

Changing navigationItem.rightBarButton isn't resulting in visual changes

For whatever reason my rightBarButton is 1. not going away when I set the rightBarButton to nil and 2. not changing when I set it to another UIBarButtonItem.
I can successfully change the title on the fly so I don't suspect the inherited navigation controller is the problem.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "Settings"), style: .Plain, target: self, action: nil)
This simply does nothing and the right bar button remains as a button with text "Cancel".
Probably you are have some problem with image: UIImage(named: "Settings"). Check the name of the image or try to use this to fill a UIImageView
EDIT:
this the code that I used to fill a item with an image and act like a button
//set frame
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(TorchView.showRank(_:)), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
let editButton = UIBarButtonItem(image: UIImage(named: "question"), style: .Plain, target: self, action: #selector(TorchView.tapQuestion))
//assign button to navigationbar
self.navigationItem.rightBarButtonItems = [editButton, barButton]
In this case I am using 2 rightButtons

Swift UIToolbar positioning

I have a UIToolbar, but I have no idea how to positioning it..
I would add UIBarButtonItems to it, and positioning them. The toolbar showing up, but i can't change it size, and i can't position the buttons in it.
override func viewDidAppear(animated: Bool) {
self.navigationController?.setToolbarHidden(false, animated: true)
self.navigationController?.toolbar.frame = CGRectMake(50, 50, 50, 50)
self.navigationController?.toolbar.sizeToFit()
let plusImg = UIImage(named: "navbar_icon-02.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let plusButton = UIBarButtonItem(image: plusImg, style: UIBarButtonItemStyle.Plain, target: self, action: "postLeft:")
let filterButton = UIBarButtonItem(title: "Feed filter", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
let leftButton = UIBarButtonItem(title: "Button 3 tst", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
var toolbarButtons = [filterButton]
toolbarButtons.append(plusButton)
toolbarButtons.append(leftButton)
self.toolbarItems = toolbarButtons
}
There are two possible problems you might be having (I can't quite tell, from the way you've asked the question).
One problem might be that you are not in a navigation interface - you simply have a "loose" toolbar. Thus, the toolbar referred to through self.navigationController? is not your toolbar, and setting self.toolbarItems has no effect - those things are only for when you are in a navigation interface and the toolbar in question is the navigation controller's toolbar.
The other problem might be that you are in a navigation interface, in which case the toolbar position is not up to you - it's up to the navigation controller, which will always place it at the bottom of the screen.
So, either you need to be in a navigation interface so that the toolbar is the navigation controller's toolbar (and then your code will work, except for the positioning part), or else you can use a "loose" tolbar, in which case you need your code to refer to your toolbar, in which case you are free to position it, and you can give it items by setting its items.