I changed back button text with below code, here domain is the string of a url which may be a very long string. So how to display it correctly when it is very long?
let backButton = UIBarButtonItem(title: domain, style: .plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem = backButton.
There is a limit set to the back button title. This limit cannot be determined through the Apple doc. If you try to set the longer title than specified limit Apple has then this longer title will be replaced with "Back" title. And it hardly makes sense to have the back button title that big, it really hampers the user experience.
Just do not use a long title, look how Apple does it, when title is too long then they use a generic back title. You will not get a good result from a long back button title no matter how you implement it...
let barButton = UIButton()
barButton.frame = CGRect(x:0, y:0, width:30, height:30)
barButton.setTitle(“BarButtonTest”, for: .normal)
barButton.backgroundColor = UIColor.yellow
barButton.layer.cornerRadius = 5.0
barButton.addTarget(self, action: #selector(didTapBarButton(_:)), for: .touchUpInside)
let rightBarButton = UIBarButtonItem(customView: barButton)
self.navigationItem.rightBarButtonItem = rightBarButton
If above answer not solve your problem then instead of using navigation bar use custom view as Navigation Bar using autolayout and take button inside it, set your title accordingly.
Related
For better understanding I created a new project and broke down my issue.
Before, I had a Storyboard with a Main Menu and from there it is possible to jump to different Viewcontrollers. One of them is embedded in a TabBarController as shown in the image.
To improve the structure, I have refactored the TabBarController to a second storyboard as shown in image2. But now I want to add some NavigationBar Items (Buttons). For each Tab I want to have a Button for some actions. Somehow the NavigationBar is gone now in the new Storyboard.
I have already found some suggestions in different Threads like
iOS 8 Swift navigation bar title, buttons not showing in tab based application
From there I tried the code
let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.green
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItem.Style.plain, target: self, action: nil)
navigationItem.rightBarButtonItem = rightButton
Which gives me a green Back Button, but the rightButton is not showing.
I have also tried embedding in a new NavigationController which just results in a double navigation...
Also I tried to add just a NavigationBar manually, which also results in a double navigation as shown in the image below.
Any ideas how I could solve this?
I don't know if this is a good solution or not but it works for me.
Basically the code was correct
let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.green
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItem.Style.plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem = rightButton
But it is necessary to add one thing to the code, adding parent solves my issue
self.parent!.navigationItem.rightBarButtonItem = rightButton
I am creating a project where I need to have a default back button and custom filter button on the left side of the navigation bar.
I have created custom back button, custom filter button and add both of them to leftBarButtonItems
navigationItem.leftBarButtonItems = alignedLeftBarButtonItems()
func alignedLeftBarButtonItems() -> [UIBarButtonItem] {
let filterButton = createLeftCustomBarButton()
// back button creation
let backButton = UIBarButtonItem(image: R.image.navBackArrow(), style: .plain, target: self, action: #selector(self.backButtonPressed(_:)))
backButton.tintColor = UIColor.white
let barButtonsItems = [
backButton,
UIBarButtonItem(customView: filterButton)
]
return barButtonsItems
}
Things are fine and working.
Concern:
Default, User can go to the previous VC by sliding from left to
right. This functionality is lost in making this. Is there a way I can
have both buttons with the sliding functionality.
You need to use UIBarButtonItem to use default left or right swipe functionality for back button:
let backButton = UIBarButtonItem(image: Image.backButton, style: UIBarButtonItemStyle.plain, target: self, action: #selector(actionBackButton))
self.navigationItem.leftBarButtonItem = backButton
With Custom button, you cannot get this functionality
I have the following snippet to add a "next" button to the top right of my nav bar.
let next = UIButton()
next.setTitle("Next", for: .normal)
next.addTarget(self, action: #selector(nextButtonHandler), for: .touchUpInside)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: next)
This is showing fine on regular size phone, but NOT on my 5C... I can't figure out why.. I put a breakpoint where the right bar button item is set and it is NOT nil.. Help!
Use this:
let barButton = UIBarButtonItem(title: , style: , target: , action: )
Add the button to the navigationController
super easy
I want to hide the text of the back button on the navigation bar and so have found past questions such as this: UINavigationBar Hide back Button Text
However I can't change the text at all, either via using the storyboard, or in code.
See screenshot below for attempt at changing it using the storyboard:
Or if I try to do it programatically by adding the following to viewDidLoad of the pushed view controller
self.navigationItem.backBarButtonItem?.title = "stuff"
It has no effect, nor does moving the same line of code to the view controller doing the pushing.
How come it won't change at all regardless of how I'm trying to change it?
How come using the storyboard, the navigation item title can be set, but not the back button text?
If I add the following to the pushed view controller then I can get the text to change:
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted)
But I would like to understand why none of the other ways of trying to change it have any effect
The title of the back button gets automatically set to the title of the view controller that it will go back to.
To do what you want, you'll have to hide the back button and insert your own button with your own image.
Annoying == #YES.
As Brett mentioned above, a new bar button must be created to change the text.
To set the title of the back button, try the following code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Stuff" style:UIBarButtonStylePlain target:nil action:nil];
Or make it in storyboard by adding a bar button item to your navigation bar.
When it comes to segue from tabBarController to a normal navigation controller, it is always easy to get confused in implementing backBarItem.
The trick is about which controller the backBarItem belongs to. If we navigate from controller A to controller B, then the backBarItem, which is the back button appearing on the controller B's navigation bar, actually belongs to controller A. So we just need to find the right controller to edit the backBarItem.
Solution 1. In the controller A, set the backBarButton self.tabBarController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
//M: In controller A
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
}
Solution 2. We can use a customised leftBarButton in controller B to cover controller A's backButton. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "<", style: .done, target: self, action: #selector(tapBackButton)), then set the action of the leftBarButton to go back to the previous controller.
//M: in Controller B
override func viewDidLoad() {
super.viewDidLoad()
//M: Hide the default back button.
//M: backBarItem will be covered by the leftBarItem anyway, here is to add an extra handling.
navigationItem.hidesBackButton = true
//M: Customize a leftBarButton.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: " < ", style: .done, target: self, action: #selector(tapBackButton))
//M: Customize the color and font size to the leftBarButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 25)], for: .normal)
}
//M: Set the action of the leftBarButton to go back.
#objc func tapBackButton(_ sender:Any){
self.navigationController?.popToRootViewController(animated: true)
}
In my Xcode Swift project, I have a view controller that has a programatically created back button for the left bar button item. The text shows up, but the back arrow is missing. How do I add a back arrow so that it matches the other view controllers in my project? I'd prefer to not have to use a custom image, but if that is the only way then I'd like to know how to do it.
var backButton = UIBarButtonItem(title: "My List", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
self.navigationItem.leftBarButtonItem = backButton
I don't think you can.
As seen here and here you'll have to create the image.
let backButton = UIBarButtonItem(customView: "yourView")
self.navigationItem.leftBarButtonItem = backButton