Add another button to navigation bar - ios

I'm modifying an app and this app has some buttons in its navigation bar, I wanted to add another button or Bar Button item by dragging it to the hierarchy but it seems I can't add it as a child to navigation bar, I wanted to ask if you have any solutions to this, here's a snapshot of the hierarchy of the storyboard, I want to add another Bar Button Item or another Button to the Navigation item.
Thanks

So I've just created a blank project and I can do this:
I've also done stuff like this before where I actually use a UIButton rather than a bar button item gaining some extra customisability. Just make sure that you set an outlet to the button and in the view controller call self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.customButton];:
You can have multiple right bar button items and if they overlap the title (if you have one) then they are essentially clipped. From my understanding, you can have as many buttons as you like until the space is gone (which can vary depending on the device, obviously). Relevant docs:
This array can contain 0 or more bar button items to display on the right side of the
navigation bar. Items are displayed right-to-left in the same order as they appear in the array. Thus, the first item in the array is the rightmost item and other items are added to the left of the previous item.
If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not
displayed.
An example with lots of buttons:
Programatically you can add buttons using:
navigationItem.rightBarButtonItems = [aButton, anotherButton, awesomeButton]
Just make sure you test that the smallest device you target will still provide a good UX for the user if you add lots of buttons.

here is the correct answer for swift :
let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)

Seems you can only have 3 elements on navigation bar with Interface Builder and you have to add more buttons via code. This link worked for me :
http://mandarapte.com/apple/add-navigation-bar-buttons-ios-app-xcode-project/

Related

Remove default slide right animation on UITabBarController from "More" Menu

I have a situation where I have enough tabs on a tab bar controller where the default system functionality has introduced the famous "..." more icon.
This is totally fine and I'm happy with the menu that is popped up when you tap it.
What I've noticed however is that a default transition occurs when selecting a item from the "more" menu (basically a slide right) to load the VC from the menu. Unfortunately this also introduces a lag on the accompanying slide "left" to get back to the menu.
My question is if this is default behaviour I have to live with or if this is something I can remove completely (ideally no transitions would be my preference).
Thanks,
Andrew
May this help you:
The More Navigation Controller
The tab bar has limited space for displaying your custom items. If you add six or more custom view controllers to a tab bar controller, the tab bar controller displays only the first four items plus the standard More item on the tab bar. Tapping the More item brings up a standard interface for selecting the remaining items.
Solution to your query:
Add only Five view controllers in your tabbar controller.
For last Fifth view controller, add more tabbar button icon (or any other image icon) manually and set a similar looking tableview (menu list).
Handle further navigation for remaining (Sixth onwards) view controllers, manually from tableview (row selection)
Storyboard Layout:
If you are looking for an alternate of more navigation, then you must customize tabbar controller and this may help you:
ZRScrollableTabBar: Scrollable tabbar items
IDScrollableTabBar: changing tabs with left and right swipe gestures on the tab bar.
JFATabBarController: Swipe tabs left and right
Ask me, if you want more clarification here.

Custom navigation back button with custom title in swift3

I need to implement the navigation bar with custom back image and custom title. For example: if i am in "Login with facebook" page i need to show the "Login with facebook" besides the custom back image. For this i am following the code like this:
let yourBackImage = UIImage(named: "back_button_image")
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationController?.navigationBar.backItem?.title = "Custom"
But I am getting the output like this:
Note: The title is of the current page.
How to achieve this?
If the back title is too long iOS will change it to 'Back'.
However if you add a left bar button item - this you can set to a longer length.
long back
From Apple docs: https://developer.apple.com/documentation/uikit/uinavigationcontroller
In cases where the title of a back button is too long to fit in the available space, the navigation bar may substitute the string “Back” for the actual button title. The navigation bar does this only if the back button is provided by the previous view controller. If the new top-level view controller has a custom left bar button item—an object in the
leftBarButtonItem
or
leftBarButtonItems
property of its navigation item—the navigation bar does not change the button title.
To do this go to the Storyboard, in Interface Builder, drag a Bar Button Item into your navigation bar and set its title.
*Note that you will need to hook this up to your view controller to pop it.
#IBAction func pop(_ sender: UIBarButtonItem) {
_ = navigationController?.popViewController(animated: true)
}
You'll find a lot of StackOverflow attempts to answer this (quite common) issue, and the best way that I've found is also rather complicated. You'll have to do this for every UINavigationController, UINavigationBar, and UINavigationItem in your app, and that's painful.
How Apple wants you to do it (as far as I can tell)
For UINavigationBars in a storyboard, set the Back and Back Mask values (in the Attributes inspector) to the name of your custom image.
For every view controller's UINavigationItem, set its Back attribute value to an empty string or single space.
Easy enough, right? Nope. For one thing, the empty string or single-space Back title isn't always honored. And what if you have dozens, or even hundreds, of view controllers? What if your application has multiple UINavigationControllers? What if you set configure your view controllers (or at least their navigation items) programmatically? Then it's not so easy.
How StackOverflow thinks you should do it
I'm not going to link to all of the SO answers that recommend this way of doing things. I'll just summarize it as:
Hide the navigation item's back button with UINavigationItem.setHidesBackBarItem(_:animated:).
Create a UIBarButtonItem with your custom back image and empty title, then add it to your navigation item's leftBarButtonItem.
Sit back and wait for user bug reports to flood in with complaints that your app doesn't support swiping to navigate back.
Or, if you want to be clever, attach your navigation controller's interactivePopGestureRecognizer to your custom left bar button item. This usually works, but it feels like a hack that's likely to break in future releases. If you're ok with that, then you can stop reading right here.
How I think you should do it
If you have a lot of view controllers (our app has 200+) and/or a mix of VCs configured programmatically and in storyboards, this may ultimately be an easier way.
Create a subclass of UINavigationBar and set this as the class for all navigation bars in your storyboards and code. (If you're doing it in code, note that the nav bar is a read-only property; it can be set only in the navigation controller's initializer.)
The UINavigationBar manages a stack of UINavigationItems, one for each view controller on the UINavigationController's stack, and keeps them in sync with their view controllers. In other words, when you push a view controller onto the nav controller, that view controller's nav item also gets pushed onto the nav bar, and it gets popped when the VC is popped. When the navigation bar pushes or pops an item, it uses the new topmost item's title, titleView, and/or button item properties to set configure its own title view and buttons, and uses the title of the navigation item immediately below the topmost one to configure its back button and back image. If the next-to-topmost item's title is nil, then the nav bar uses the default value of "Back". You don't want that. If you can set the Back attribute to an empty or single-space string in the storyboard, then you should be able to do the same thing in code, right? Wrong. The back text is not a public property in UINavigationItem! WTF?
Instead, you have to set the title of the next-to-topmost item to an empty string. "But wait!" you say, "Doesn't that permanently change the next-to-topmost item's title, so that when I navigate back to that view controller, its title will say "Back" instead of its original title?" And you'd be right. What can you do? In your subclass of UINavigationBar, add an array of Optional title strings, like so:
var navigationItemTitles = [String?]()
(Why Optional? Because navigation items may have a custom titleView instead of a title.)
Override the navigation bar's pushNavigationItem(_:animated:) and popNavigationItemAnimated(_:) so that they modify and restore the navigation items' titles, respectively:
override func pushNavigationItem(_ item: UINavigationItem, animated: Bool) {
super.pushNavigationItem(item, animated: animated)
// Store the new back item's title, then set its title to
// an empty string.
if let backItem = backItem {
navigationItemTitles.push(backItem.title)
backItem.title = ""
}
}
override func popNavigationItemAnimated(_ animated: Bool) {
super.popNavigationItemAnimated(animated)
// Restore the new top item's title.
if navigationItemTitles.count > 0 {
topItem?.title = navigationItemTitles.popLast
}
}
Caveats
This is the only way I've found that's foolproof and not hacky. If Apple simply made the navigation item's Back property publicly settable, then a lot of this could be avoided.
(For what its worth, not only does our app have 200+ view controllers, it also has multiple navigation controllers with different navigation bar looks and feels. That's a whole other layer of complexity.)

How to swap position of tab bar item when refactored storyboard

When i refactored storyboard same photo in this
link.
I can't find where i can swap position of tab bar item.
example i want to change main menu to index 0.
this is my storyboard.
You can just re-arrange the order by dragging and dropping the icons in the tab bar controller
If you are using custom tabs, you can still drag and drop to reorder even though the Tab Bar Controller scene shows a grey bar, the bar item is still there. This is especially inconspicuous when all your bar item are custom items.
Remove all relation and start making from MainMenu to what you want at the end.

Add logo to left side of Back button in navigation bar

I am trying to add a logo to the very left side of my navigation bar. I still want to display the Back button and the title of the page, but the logo must be fixed to the left side before the Back button.
I tried adding the logo as a Bar Button Item, but this removes the Back button.
Is there any way to do this? Thanks in advance!
If you add a UIBarButtonItem as the leftBarButtonItem then you can also set the property
self.navigationItem.leftItemsSupplementBackButton = true
This will allow you to have two buttons without disrupting the back button. However it won't be on the far left, that space is reserved for the back button. If you want to go against Apple's design guidelines and the logo is more important the standard iOS navigation, you'll need to do something custom.
You could use the leftBarButtonItems property on UINavigationItem to set multiple items in your own order. One of those could be a logo and another a custom back button that you set to call navigationController.popViewControllerAnimated(true)

Disable "Edit" Button that appear in more selection of tab bar ios

I am new in iOS. I use Seven Tab bar item in my tab bar controller story board and when run app then it shows more buttons when I click on it it is also show edit button. I do not want that edit button. How can this be done?
This is my code:
[[[tabBarController moreNavigationController] visibleViewController] setTitle:#""];
But it does not work.
You had to say that none of your view controllers is customizable. Then the edit button disappears. Please look up the docs.
tabBarController.customizableViewControllers = #[];
Docs: "This property controls which items in the tab bar can be rearranged by the user. When the user taps the More item on the tab bar view, a custom interface appears displaying any items that did not fit on the main tab bar. This interface also contains an Edit button that allows the user to rearrange the items. Only the items whose associated view controllers are in this array can be rearranged from this interface. If the array is empty or the value of this property is nil, the tab bar does not allow any items to be rearranged."
On Swift: You can also have those items you want to be editable in the array:
tabBarController.customizableViewControllers = []
Ensure that if you have a navigation controller for the tabbar controller that it is not translucent because the more navigation bar will be behind the main navigation bar. This sometimes shows an empty space above the more tableview.
You can completely hide the more navigation controller if you do not need it, since tapping the more tab pops the top view controller.
tabBarController.moreNavigationController.navigationBar.isHidden = true

Resources