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!
Related
Can someone clever explain me why this is happening?
I have a ViewController A and ViewController B where A does push() and B is on top.
Inside A I have created this code:
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
This gives me this crazy output :) It's like default iOS arrow + mine next to each other.
At least it does not have "Back" title which is also something that I need :)
When I change my code to something like that:
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
I've got arrow (and can modify it's tint), no title but it's not the same as my custom image.
Why is my custom image simply not replacing the system stock one?
First I would hide the default back button using
self.navigationController?.navigationItem.hidesBackButton = true
And then I can proceed and add a new custom back button on the left items
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
you can set custom back navigation button by below code :
private lazy var backBarButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: nil, action: nil)
button.tintColor = .white
return button
}()
self.navigationController?.navigationItem.hidesBackButton = true
self.navigationItem.backBarButtonItem = backBarButton
I´m trying to make a new custom navigation bar button with my own image. That´s working fine, but I´m trying to open a new view controller when I press that button. Unfortunately that doesn´t work and I have no idea how I should do it.
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "Image.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem = testUIBarButtonItem
func clickButton(sender: UIBarButtonItem){}
That´s my code right now and its inside my viewDidLoad. My intention is to perform a show detail Segue comment.
Can someone help me ?
why don't you just use a UIButton and assign it as bar button item as suggested in this thread:
UIBarButtonItem: target-action not working?
let button = UIButton(type: .Custom)
if let image = UIImage(named:"icon-menu.png") {
button.setImage(image, forState: .Normal)
}
button.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
button.addTarget(self, action: #selector(MyClass.myMethod), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton
If you are using a Storyboard, add a manual segue to your ViewController and give it an identifier. Then you have to call performSegue in your custom navigationbar button's IBAction.
func clickButton(sender: UIBarButtonItem){
self.performSegue(withIdentifier: "yourSegueIdentifier", sender: nil)
}
I try to add refresh button programmatically to my NavBar, while I am in TabBarController and I have a big problem with that. This is my code in ViewDidLoad():
let buttonItem = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: "refreshData")
self.navigationItem.rightBarButtonItem = buttonItem
any suggestions?
This is my storyboard after launch my app (no icon):
Make sure you viewController embed in UINavigationController first then add this navigation controller into tabViewController's view controllers array. After that use the method below:
let refresh = UIButton(frame: CGRectMake(0, 0, 40, 40))
//Set refresh image
refresh.setImage(UIImage(named: "refresh"), forState: UIControlState.Normal)
//or set text title
refresh.setTitle("Refresh", forState: .Normal)
refresh.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: refresh)
EDIT:
what's your navBar? It's UINavigationBar, yes, it's possible:
let item = UINavigationItem(title: "")
item.rightBarButtonItem = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Done, target: self, action:"refresh")
item.hidesBackButton = true
UINavigationBar().pushNavigationItem(item, animated: false)
But normally it's should be embed in UINavigationController
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.
Hi there I'm not able to change the navigation bar default back button's title
I've tried that :
self.navigationController?.navigationBar.backItem?.title="retour"
self.navigationController?.navigationItem.leftBarButtonItem?.title="retour"
self.navigationItem.backBarButtonItem?.title="retour"
self.navigationItem.leftBarButtonItem?.title="retour"
Set as leftBarButtonItem
var backBtn : UIBarButtonItem = UIBarButtonItem(title: "retour", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = backBtn