Well, back bar buttons title changes to "Back" when title of the previous viewController is too long to fit the space within the button.
I want to change the title to something else when it says "Back", so I thought I can do something like that:
if navigationItem.backBarButtonItem.title == "Back" {
navigationItem.backBarButtonItem.title = "SomeThing"
}
But title (or its parent view) always returns nil.
Also, where to put the code: in the pushing or pushed viewController?
Thanks.
EDIT: The approved answer in the link
How to set back button text in Swift
is different, because I want to change the title only in the case it is too long to fit inside the button label.
Related
First let me explain something, so you guys can understand better my problem:
I'm using Toolbar and i have a Fragment called Location Fragment where i have 6 floationg action button vertically .
When you click on one of the floating action button some action like geofence,live tracking,current location etc are performed on the same map fragment.
So my problem is that when i click any of the floation action button ,like when i click geo-fencing floation action button the title on the tool bar should change with the geofence title..In this way for all the Floating action button the title of toolbar should change. in my case the title of the tool bar is not changing .
I have tried to use setTitle or this.Activity.Title="Geo-fence" on the click method of Floating action button,but still it remains same title .
Change ToolBar Title dynamically on click events in Fragment
In your Fragment, add the following code in your click events :
((AppCompatActivity)Activity).SupportActionBar.SetTitle(Resource.String.YourTitle);
this.SupportActionBar.Title = "Your Title";
This question already has answers here:
How to change text on a back button
(20 answers)
Closed 6 years ago.
I just want to keep back icon but not back text which used to coming with every view whenever we push any viewcontroller. I don't want to keep any text with back icon. I just want to show only back icon without any text and without using any specific image.
I don't want to edit text of backbutton title. I want to completely remove text from backbutton icon.
In the interface builder set the back text to a space (" ").
You can use this.
self.navigationController.navigationBar.backItem.title = #"";
if you are using storyboard then you can chage on navigation controller back button item
I have a navigation controller stack where one of the views has a dynamic title.
The view controllers and their titles go like this:
Main --> ItemsTableView --> ItemDetails
Title:Main Title: NN Items Title: Details
Because the iOS UINavigationController sets the text of the "Back" button to be the title of the previous screen, the "Back" button on the details screen says "< NN Items" where NN is a dynamically changing number.
I'm trying to do some iOS UI automation, but the accessibility Label / ID of the back button is set by the system to it's button text. This means that the accessibility label of the back button on the details screen will change dynamically, and I can't find it from my scripts!
If I could get a reference to the UIBarButtonItem then I could easily set it's accessibilityLabel or accessibilityIdentifier from code to be a fixed string, however I can't figure out how to do this?
All of the stuff I've been able to find references setting the back button to a custom button via self.navigationItem.backBarButtonItem or similar, but when I read this property it's nil. I haven't been able to find out how to get access to the standard item without replacing it. I'd prefer not to replace the button if possible
This was bugging me as well. I've been writing Xcode 7 UI Tests and was trying to come up with a generic way of tapping on the back button without having to replace it with a custom button.
The following is how I solved this for Xcode 7 UI Tests - but you may also be able to apply this to UI Automation as well.
I discovered that (in terms of Xcode 7 UI Tests at least) the back bar button item that is created by the system consists of two buttons the entire thing is a button with an accessibility label of whatever the title of the button is, and then the arrow is also a button with an accessibility label of "Back".
Thus, as long as there aren't any other buttons on the screen that are identified as "Back", the back button can be accessed via the accessibility label of "Back". Like so in the case of UI Tests:
[[app.buttons matchingIdentifier:#"Back"] elementBoundByIndex:0]
Here I'm getting the first button that can be identified by "Back". I my case there could only ever be two such buttons - the arrow, or the whole back button itself (in the case where the back button's title is also "Back"). Since both of these buttons are essentially the same, just getting the first one it finds is sufficient.
If I call UIButton:setImage:forState then the title text of my buttons is disappearing (set via UIButton:setTitle:forState).
If I don't call setImage:forState then the title appears perfectly.
If I set the button images in interface builder then the title does appear, so why does setting the images programmatically cause the titel to vanish?
Instead of calling setImage:forState:, you should call setBackgroundImage:forState:.
Is it possible to see the selected state content of an UIButton only when I'm touching it?
When you're touching a button, the button is highlighted. Call setTitle:forState: to give the button the normal title (UIControlStateNormal), and call setTitle:forState: to give it the highlighted title (UIControlStateHighlighted). Now you'll see one title normally and the other title when you touch the button. The same thing for the other button features, like setImage:forState:.