Avoid showing "back" for back button when navigation title is long in Swift - swift

There is a navigation in my project which I want to config it's navigation back item.
First case: When the UINavigation title is long the title of back button is set to "back"
replacing back button title to "back"
Second case: when it is longer this "back" is not shown.
delete back button title and just show back icon
But I want to show just back icon in the first case too.

Swift 3 - Through Storyboard:
To make navigation bar back button have only back arrow and no "Back" text written, follow the steps:
Go to navigation bar of the root view controller(titled "Home" in screenshot below)
Go to its attribute inspector. Set the back button to a space as shown below:
And that's it!!
This is the simulator screenshot:
Hope it helps!

Add this in viewDidLoad of ViewController which is pushing next ViewController (View Controller Linguini Arabbiatta)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
It will show just a back icon in all the View Controllers which are pushed from that View Controller.

Try this code
if let title = self.navigationController?.navigationBar.backItem?.title {
if title.characters.count > 5 {
self.navigationController?.navigationBar.backItem?.title = "Anything Else"
}
}

Here's Objective-C
[self.navigationItem setHidesBackButton:YES];
Here's Swift
self.navigationItem.setHidesBackButton(true, animated:true);
Edit: if you wish to remove the text only, then here's Objective-C
[self.navigationItem.title = #""];
Here's Swift
self.navigationItem.title = ""
Then you'll need to refresh the nav bar
self.navigationController?.navigationBar.setNeedsLayout()
self.navigationController?.navigationBar.setNeedsDisplay()

Related

how does instagram's custom navigation bar created?

I'm working on an iOS app and gonna work on the navigation controller part.
The UI of the navigation bar is not complex in my app; basically, it should have an app logo on the left an app name in the middle, and two bar button items on the right.
While I was researching, I saw the Instagram's navigation is sort of similar to what I want to try, especially the "Instagram" logo on the left.
I'm not sure if I can make a navigation bar like an instagram's navbar with a default UI navigation bar, so I guess I need to make a custom navigation bar with .xib file or something? or is it possible to make it with the default navigation bar and some codes??
If you can walk through the approach you take, please let me know...
This can be easily done with a default UINavigationController. For the logo you can create a UIBarButtonItem and set the image to whatever image you want (e.g a logo).
I have a function that sets the collection view's offset to 0 when tapping on the logo similar to Instagram.
For the 3 buttons on the right, you can do something like this:
self.navigationItem.rightBarButtonItems = [button1, button2, button3]
Each button would be their own UIBarButtonItem and then add a selector to each of them.
Here is something similar to what I have in my app:
let calendarButtonItem = UIBarButtonItem(
image: UIImage(
systemName: "calendar.badge"),
style: .done,
target: self,
action: #selector(handleCalendarTap))
let navLogoButtonItem = UIBarButtonItem(
image: UIImage(named: "nav-logo"),
style: .plain,
target: self,
action: #selector(handleNavLogoTap))
self.navigationItem.rightBarButtonItem = calendarButtonItem
self.navigationItem.leftBarButtonItem = logo

How to pass Data from one view controller to another view controller without using seague and storyboard

Please Do it Without Using Storyboard.
when I click on radio button in the popup.When I click on 'Ok' The title of the radio button will be the button title in main view controller.
You can get title from UIButton.isSelected property. You can set title in viewDidLoad method like
YourButton.setTitle("Title", for : .normal)
YourButton.setTitle("Selected Title", for : .selected)

Push Navigation Controller Without Title On Back Button

I currently have view controller A and view controller B.
View Controller A pushes vc B onto it's navigationController on a button press:
navigationController?.pushViewController(viewControllerB, animated: true)
VC A has a navigation bar that has a title on it "View Controller A Title"
I want VC B to have a left button, which is a back button, and a title, "View Controller B Title"
Currently, when I push VC B onto the nav controller, the title of VC A comes along with it onto the left back arrow, so it looks like
< View Controller A Title View Controller B Title
How do I get rid of the VC A Title?
I've tried setting left bar button item, back bar button item, both with no success.
The only way I've found to remove that title is to do
navigationController?.navigationBar.items?.forEach({ $0.title = "" }) inside of the viewDidLoad of VC B, but then when I click the back arrow, the title is of course missing from VC A
The only solution I've found for this solution listed above here, is to set the navigationItem.title in the viewDidAppear of VC A, but there's about a 1/2 second delay from the title actually showing up which is not ideal.
Is there a better way to not have this title come along with the navigation left bar item?
Thank you in advance!
In View Controller A, just set the backBarButtonItem.
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

Left Bar Button Item - show name of previous View Controller

If you have a Navigation Controller with no Bar Button Items, a navigation back button will be shown with the name of the last View Controller.
I want to keep that name, as in I don't want to have to hardcode it. I do know how to add it in but I don't want to have to do that because that leaves more room for bugs.
Is there a way that I can have a left Bar Button Item and for the default one to not go away?
Add this in viewController where you want to have default back button and custom bar button item. You can customise the bar button item.
override func viewDidLoad() {
super.viewDidLoad()
let newBtn = UIBarButtonItem(title: "new", style: .plain, target: self, action: #selector(anotherMethod))
self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItem = newBtn//self.navigationItem.leftBarButtonItems = [newBtn,anotherBtn]
}

Swift can't show back button after hiding it

I have a UITableViewController with static cell that has a custom back button. I hide the back button in viewDidLoad with the code:
var cancelBtn = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "closeSettings")
self.navigationItem.hidesBackButton = true
self.navigationItem.leftBarButtonItem = cancelBtn
If I don't have the self.navigationItem.hidesBackButton = true line in place, the back button arrow still appears. With that line in the code, the back button is hidden and the Cancel button appears exactly how I want. The problem is coming when I open another view controller from a cell, the back button is not present. I instantiate the view controller when a cell is clicked, and use the Navigation Controller to push the View Controller. I have attempted to put
self.navigationItem.hidesBackButton = false
in the code right before pushViewController(). When I do that I see the back button appear, but when the new View Controller is pushed on the Navigation Controller, the back button disappears.
I have even attempted putting the hidesBackButton = false line in the target View Controller's viewDidLoad and viewDidAppear, but I was not able to get the back button to appear. Could anyone help steer me in the right direction on what I could be doing wrong?
Did you try setting self.navigationItem.hidesBackButton = false at viewWillDisappear()?
Try this setting
self.navigationItem.hidesBackButton = false
on the destination view controller in the ViewWillAppear section.