I'm using the newest version of Xcode and Swift.
I set the back button in my navigation bar as follows:
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goBack(_:)))
This shows the word Back in the upper left corner.
Is there a way to show the Apple native back button arrow without any text instead?
Or, do I have to load a custom back icon myself?
Use image like below.
let btnBack = UIBarButtonItem(image: UIImage(named: "Back"),
style: .plain,
target: navigationController,
action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItem = btnBack
navigationController?.interactivePopGestureRecognizer?.delegate = self
Related
i am setting large title to navigation bar and then on right side i am adding two buttons but button not set to the center with large title my code is as below
Code
navigationController?.navigationBar.prefersLargeTitles = true
title = "Record"
let settingImage = UIImage(named: "ic_Setting")!
let infoImage = UIImage(named: "ic_Info")!
let settingButton = UIBarButtonItem(image: settingImage, style: .plain, target: self, action: nil)
let infoButton = UIBarButtonItem(image: infoImage, style: .plain, target: self, action: nil)
navigationItem.rightBarButtonItems = [infoButton, settingButton]
i put above code inside viewDidLoad() and here is my screen shot which out put i am getting form above code and other screen shot which is my expected output
This is my output i get from above code:
I want output like this:
so please help me how to center buttons with title
I'm trying to remove the text "Back" from the navigation back button, leaving just the back chevron, but everything I'm trying is not working. For example if I add something like the following, obtained from previous answers to the same question, to viewDidLoad:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "go away", style: .plain, target: nil, action: nil)
or
navigationController?.navigationBar.backBarButtonItem = UIBarButtonItem(title: "go away", style: .plain, target: nil, action: nil)
Then when the view appears it's still showing "< Back" in the navigation bar.
Here's what the views look like within captured within viewDidAppear.
Image:1
Try this code snippet hope it will help you
happy coding =)
override func viewDidLoad() {
DispatchQueue.main.async {
if let navBar = self.navigationController?.navigationBar {
navBar.backItem?.title = ""
}
}
}
You should create a left button and set the action to return to the rootViewController.
In viewDidLoad:
let leftButton = UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(back(_ :)))
self.navigationItem.leftBarButtonItem = leftButton
You are changing the wrong thing. You use this code here to change the title for the back button.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .done, target: self, action: #selector(handleBack))
By doing this you need to add a selector for the button as well. Cause if you click the back button nothing will happen. This is how you would do that.
#objc private func handleBack() {
navigationController?.popViewController(animated: true)
}
Hope this helps.
Alternatively, from Interface Builder, you can set previous UIViewController's Back Button on Navigation Item to " " (not empty string, space):
I am new to iOS (swift) programming. I am creating my first stop watch application. I have added a navigationBar to the MainStoryBoard, and then two navigation items on the bar, one on the left and one on the right. I would like to change the text of left navigation item in the viewDidLoad method and then in an IBAction. Can anyone help me please.
With my .NET coding intellect, I created IBOutlets for each of them and then tried changing their title properties but have failed.
Another similar question regarding the same is how can I toggle the icons on navigation items programmatically.
For UIBarButtonItem with custom button image:
let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
Set Right/Left Bar Button item:
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
For System UIBarButtonItem:
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera
You should don't create UINavigationBar on StoryBoard. You should ember it into UINavigationController. And in viewDidLoad you can add NavigationItem programmatically.
Add NavigationBarItem just with text:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "left", style: .Done, target: self, action: "leftTap")
With Image:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "backIcon#3x"), style: .Done, target: self, action: "rightTap")
And in Action you can change text or image:
self.navigationItem.leftBarButtonItem?.title = "left change"
self.navigationItem.rightBarButtonItem?.image = UIImage(named: "calendar-icons#3x")
With title and image above just is demo. You can reference to my demo code: Demo Change NavigationItem Programmatically
Hop this help!
From past few days i am trying to generate a popover for programmatically added UIBarButtonItem but i couldn't succeed. Added to this, i even want this popover to be presented with few images sequentially which are clickable. The following is the code of how i generated a UIBarButtonItem programmatically
func imagerendering(){
let barbuttonimage = UIImage(named: "app")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let button1 = UIBarButtonItem(image: barbuttonimage, style: .Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = button1
let attachButtonImage = UIImage(named: "icon-Attach")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let attachButton = UIBarButtonItem(image: attachButtonImage, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.setRightBarButtonItems([menuButton,attachButton], animated: true)
let fixedSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
fixedSpace.width = 5.0
self.navigationItem.rightBarButtonItems = [menuButton, fixedSpace, attachButton]
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Black", size: 16)!]
}
I have presented my desired output in an image as a link below. please go through it and let me know whether what i am desiring is possible or not.
desired output image
To add an extra bar item on navigation bar using storyboard, you can refer these answers
After that, set a popover from storyboard(you know it well as you said) and then put required buttons with actions in your popop view. You can set image for a button instead of title text.
Look into the WYPopoverController library. It gives you the functionality you're looking for.
In an iOS app I am making, I have set the identifier of a bar button item programmatically using:
homeTitleBar.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Trash, target: self, action: nil)
Which changes the button to a trash can.
My question is, after that, how can I then change it to a custom image.
homeTitleBar.leftBarButtonItem?.image = UIImage(named: "settingsIcon")
does not seem to be working.
Try
homeTitleBar.leftBarButtonItem = UIBarButtonItem(image:UIImage(named: "settingsIcon"), style: UIBarButtonItemStyle.Plain, target: self, action: nil)