How to set navigationBar back button image to default symbols? - swift

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)
}

Related

Adding a UIBarButtonItem programmatically

I'm trying to add a UIBarButtonItem programmatically.
let navigation = UINavigationController()
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
navigation.navigationItem.rightBarButtonItem = rightBarButton
And also made a selector function for testing.
#objc func logInPressed() {
print("go to login")
}
Unfortunately that does not work - bar button is not visible on navigation bar in simulator.
Checked with a breakpoint, rightBarButtonItem exists.
Probably issue can be caused by creating bar button from app coordinator, not from child VC.
Could please anyone help to troubleshoot this issue?
Simulator screenshot
you are adding UIBarButtonItem to a new instance of NavigationController. so it will not appear there.
so in your view controller which you want to handle right navigation bar , under one of these methods:
override func viewDidLoad()
or
override func viewWillAppear
add:
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
self.navigationController?.navigationItem.setRightBarButton(rightBarButton, animated: true)
Issue was caused by creating bar button not from child VC, but coordinator.
Bar buttons are stored in navigationItem property of UIViewController.

Using UINavigationItem on UITabBarController

I have a project with a UITabBarController and a couple of views. Sort of like this:
In this project, the tab bar controller is called by tapping the Tab button on the UIViewController. I would like to get rid of the back button with "Title", and replace it with an "X" icon. When tapped, the "X" icon would dismiss the UITabBarController and return to the caller. I do this all of the time on UINavigationController using a UINavigationItem, but that does not appear to work in this situation. I drag the UINavigationItem to the view and it allows it, but it does not show up on the view and any UIBarButtonItem that I drag and drop on it do not appear.
Is there a way to actually do this? I'd even be ok with leaving the existing back button as it is and just getting rid of "Title"
I figured it out right after posting the question. Just a bit more research is all it took.
To fix this, add the following to the UITabBarController code.
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
#IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
This gives me a black back button with my image. When I tap it, it brings me back to the calling 'UIViewController.

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.

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.