Navigation Bar Back Button - ios

I have a navigation bar that i want to use this code to change the text but unfortunately the arrow also disappears. So I added a less than sign and it worked but the size of it does not match. Is there anyway to fix this issue?
Here is my code
let backButton: UIBarButtonItem = UIBarButtonItem(title: " < Timeline", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationController?.navigationBar.topItem?.leftBarButtonItem = backButton

Create a transparent view.
Add an imageview of fixed size for the arrow image or you can use UILabel too as its working in your case.
Add another UILabel adjacent to your < label or imageview. You can use padding to make it look uniform.
Add both label or imageview on your transparent view.
Create UIBarButtonItem of your customView and then add it to your navBar.

Related

How to change UINavigationBar Title and Back Button Colour from Storyboard?

What is the best way of changing colors for Title and Back button of UINavigationBar from Storyboard?
What is the best way of changing colors for Title and Back button of UINavigationBar from Storyboard?
The color of the title is an attribute of the navigation bar, so select the navigation controller's navigation bar:
and then look at the Attributes Inspector, where you can set the title color:
The color of the Back button is controlled by the tint color. You can set the global tint color in the File Inspector for the storyboard:
These settings should work fine if you want to set the title and tint color once for the whole app, but if you want different colors for different view controllers, then one way or another you're going to have to write some code. If that's something you need to do often, and you want to be able to set the colors in IB, you could consider writing your own UIViewController subclass from which all your view controllers are derived. Give that common controller class inspectable attributes for the colors you want to set, and of course add code that sets them appropriately. You'll probably want to use UIAppearance for that.
Bear in mind, though, that the reason that these colors aren't already attributes of UIViewController is that the colors are supposed to help give your app a consistent appearance; changing your app's color scheme from one scene won't be a favor to your users.
you can do it by code
navigationController?.navigationBar.backgroundColor = UIColor.red
navigationController?.navigationBar.isTranslucent = false
let button1 = UIBarButtonItem(image: UIImage(named: "search.png"), style: .plain, target: self, action: #selector(barBut)) // action:#selector(Class.MethodName) for swift 3
self.navigationItem.rightBarButtonItem = button1
navigationController?.navigationBar.tintColor = .white

Swift 4 Change UIBarButtonItem System Icon not working

I have two UIBarButtonItems inside my Navigation Item's Right Bar Button Items. I am looking to changing the second UIBarButtonItem only programmatically.
I have created an outlet for this UIBarButtonItem:
#IBOutlet var downloadButton: UIBarButtonItem!
and then I tried to change it like so:
self.downloadButton = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(PlayerController.downloadButtonPressed(_:)))
But the UIBarButtonItem does not change over, what am I doing wrong?
But the UIBarButtonItem does not change over
There's no reason why it would. All you're doing is replacing the value of a variable downloadButton with a new bar button item. This has no effect on the bar button item still sitting there in the navigation bar — plus, you have destroyed your reference to that bar button item.
You have to reconfigure your navigationItem. You can also set your downloadButton again if you like, but more likely you'll realize you never needed the downloadButton outlet for anything in the first place.
Here's an example from one of my own apps, where I toggle the right bar button item between being a Play button and being a Pause button:
let whichButton : UIBarButtonItem.SystemItem? = {
switch self.mp.currentPlaybackRate {
case 0..<0.1:
return .play
case 0.1...1.0:
return .pause
default:
return nil
}
}()
if let which = whichButton {
let bb = UIBarButtonItem(barButtonSystemItem: which,
target: self, action: #selector(doPlayPause))
self.navigationItem.rightBarButtonItem = bb
}
Notice the last line: I just reach right in and change the navigationItem. Instantly the navigation bar display changes to match.

Cann't I set title and Image to the right bar item on navigation bar at the same time in storyboard?

I want to set the right button item to be this:
It has the title and the down image.
But in storyboard, I can not do that:
I only can set one of the title and image, if I set image, I can not set the title, if I set the title, I can not set the image.
I have tried use the UIButton and the Button Bar Item to do it, get the same result.
Some friend know how to do with that?
You can simply add drag UIButton to rightBarItem in storyBoard . and customise that button as per your needs ..
Check View hierarchy after adding UIButton .
you can try this add the custom view to your bar button item
var RightView = UIView(frame: CGRectMake(0, 0, 100, 44))
// you can add in RightView, your title in uilable and your image in imageview
var RightBarButton = UIBarButtonItem(customView: RightView)
self.navigationItem.rightBarButtonItem = RightBarButton

iOS: Remove gap between left uibarbuttonitems

I had setup two UIBarButtonItem on the left. Below is the screen shot of the wireframes of the screen, captured from debugging view hierarchy. Red box is the default back button and green box is the menu button.
From the screenshot, there is a gap between the back button image and menu button. The back button's view is occupying the extra space. I'm trying to figure out a way to get these two button close to each other.
I removed the "Back" text for the back button:
let backItem = UIBarButtonItem()
backItem.title = ""
self.backBarButtonItem = backItem
And added menu button:
let btn = UIBarButtonItem()
btn.customView = menu // it's a UIButton
self.leftItemsSupplementBackButton = true
self.leftBarButtonItem = menu
If it truly is the back buttons view, then just reduce the size of its views frame and you are good to go.
If it is an attribute of the main back bar button item they give you, then make a custom one that looks the same and give it the appropriate size.
If you are using a flexible space bar button item, then use a fixed space bar button item and set it appropriately.
You can also modify the value of a bar button view's location through the insetInPlace() that you use on the frame, but that will take some experimenting on the correct values to be used.
There are few options:
One is to insert an invisible bar button item and give it negative width like shown here https://stackoverflow.com/a/31804099/520810
Alternatively you can adjust image insets https://stackoverflow.com/a/22742322/520810

Reference the back button on UINavigationBar

I need a reference to the back button in the UINavigationBar or UINavigationItem. The Back button in the image below.
I am not customizing the button, thus
navigationItem.backBarButtonItem
navigationItem.leftBarButtonItem
these both are nil.
Is there any way to get a reference to the button without customizing it?
Reference:
backBarButtonItem
leftBarButtonItem
Update: My comment might help to understand why I don't want to customize the back button. :)
The Documentation says this about the backBarButtonItem:
When this item is the back item of the navigation bar—when it is the
next item below the top item—it may be represented as a back button on
the navigation bar. Use this property to specify the back button. The
default value is a button displaying the navigation item’s title.
So the backBarButtonItem is always nil by default because it belongs to the previous view controller. The only way to get a non-nil reference is by customizing it.
If you just want to change the name, however, that can be done in the previous View Controller.
In reference to my use case - Ayush's comment
Till the time I get a concrete solution, I have created a dummy view below the navigation bar (below the back button) to show the tip from. Here is the code.
let v = UIView(frame: CGRectMake(0, 0, 50, 0))
view.addSubview(v)
let tipText = "Here it the back button"
EasyTipView.showAnimated(true,
forView: v,
withinSuperview: view,
text:tipText,
preferences: nil,
delegate: self)
try out this one
let backItem = UIBarButtonItem(title: "Custom Text HERE", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem
Try this it might help you.
UINavigationItem *i = [self.navigationController.navigationBar.items firstObject];
i.title = #"Hai";

Resources