Add logo to left side of Back button in navigation bar - ios

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)

Related

Add another button to navigation bar

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/

How to set action for the bottom UIBarButtonItems in objective C?

I have an application where there are two items at the bottom bar in a view controller. One item is at the left and another one is at middle of the bottom bar. Now, what I like to do that when someone click on the middle item of the bottom bar, it will redirect the user to the website. how to get the action for the middle item of the bottom bar.
If this is a UIBarButtonItem, it has a target property and an action property, These configure what it does when tapped — it sends the action to the target. In theory, you should have configured these when you created the UIBarButtonItem.

ios swift How to add images to navigationbar that are not in the center/titleview?

In swift, how do I add an icon to the navigation bar on the side instead of the middle?
I want to do what Slack does with their search icon by having it appear just next to the right menu icon, see picture here: https://static-s.aa-cdn.net/img/ios/618783545/1b2dca063c580767bf28c885e22c61bc
I DO NOT want the icon to be in the center of the navigation bar with the title view property, which is what most people do. How do I have the icon appear next to the right menu? Thanks.
Depending on the complexity of your requirements you have 2 options:
Create your custom navigation bar (is just a UIView subclass)
Create an image ad apply it as a background of the navigation bar
I have not tried this with Swift yet but this works on Objective-C. You can add multiple items to the right by passing an array of UIBarButtonItems to [self.navigationItem setRightBarButtonItems:]
you can subclass it and personalize your navigation bar, this tutorial is good for that:
http://www.ioscreator.com/tutorials/customizing-navigation-bar-ios8-swift

Why can I not drag UIBarButtonItem onto navigation bar?

I'm having a little bit of an issue. When I place the UIBarButtonItem on the tab bar controller it works perfectly, it displays as it should:
However, I can't put a button onto where the black square is. Does anyone know how I could do this non-programatically?
You should probably add a Navigation Item by dragging it from the Object Library to the place where you have marked in black and then try adding a Bar Button Item on the right. It should work
UINavigationBar wants only UIBarButtonItem. You can create one with custom View programmatically. Show here: https://stackoverflow.com/a/16058345/717193

Should I use a tab bar or button at the bottom of my ViewController?

I'm just learning iOS, and I want to create an App which will have a few buttons at the bottom of the screen.
What I'm a bit unsure about is, I know you can use a tab bar down there, but is that what you should always use when you want a button at the bottom of the screen? or there's no need to use a tab bar, and you can just put a normal button down there?
According to apple's documentation, UITabBar is a control used for displaying views.
A tab bar is a control, usually appearing across the bottom of the screen in the context of a tab bar controller, for giving the user one-tap, modal access to a set of views in an app.
If your goal with this "button" is to display other views, then you should use UITabBar component.
But if you are just searching for a "usual button", then you should use UIButton component.
A tab bar is expected to allow the user to switch between, you know, tabs; the same bar appears at the bottom of each page, and it allows you to switch between them. If that describes what you are trying to accomplish, then it would be appropriate. If your button is meant for some different purpose, then a tab bar might be misleading.
A tab bar (class UITabBar) is usually part of a tab bar controller (class UITabBarController). A button (UIButton is simply a way to respond to a tap or other actions within the button.
You want to use a tab bar and tab bar controller when you need to switch between different views and view controllers in your application. For example the Music app on your iPhone has a tab bar controller that switches between Artist, Playlist, Album, etc. These are different screens, or screens that look the same but show your music organized in a different way.
If all you want is to respond to a button, for example to print out to the console or show a message to the user that says "Hey, you've tapped the button", then a UIButton is what you need.
Also, a UIButton can have many actions, Touch Up Inside is probably the one you are looking for. This one will ensure the button has an action called if the user began a tap on the button, and let go of their finger while still on top of the button.
To summarize things:
Use a UIButton if you simply want to respond to an action, and the most common action you will connect to the button is Touch Up Inside.
Use a UITabBarController to have a way to switch between different views and view controllers.

Resources