In a UIViewController, I'd like to show two tabs in a UINavigationBar. I want the UINavigationItems to have similar behavior to UITabBarItems in a UITabBarController. I want the NavBar to be visible in both view controller tabs, and I want to change the color of the tab title in the navBar if the tab is selected.
Is there a simple and elegant way to conveniently set text/selectedText or image/selectedImage and create UINavigationItems that have behavior similar to UITabBarItems, including the ability to have the nav bar space the items out evenly, just like in a UITabBarController?
It would be great if I could set this like it can be done in a tabBar:
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:titleBlack selectedImage:titleRed];
The goal is to have two tabs, that change color if they are selected and they would be evenly spaced across the bar. I know I could do something simple like add two items to a navBarArray, but in order to have them spaced evenly across the bar for all screen widths, how would I do that? Is there an elegant solution?
This example shows the first tab selected. If I select the 2nd tab, I want Tab2 to be red and Tab1 to be black. This is just text, but if I have to create an image to accomplish this, I will.
This question is not a duplicate of the many questions on S.O. that are referring to displaying both a UITabBarController and a UINavigationController in the same app. This is different because I don't want a UITabBarController(I already have one that does something else- but I don't want to overcomplicate my question)- I just want to mimic this behavior, inside of a UINavigationBar. Please please please do not tell me I shouldn't do it this way. That's just another way to say "I don't feel like helping you figure this out."
I've tried looking for clean, simple, elegant solutions. I haven't found any. A complete example would be appreciated. Thanks =)
I came up with a solution that works well enough for me. I created a custom label with 2 buttons and a view with a height of 1 to act as a separator. I moved the UITableView down to pin the top of it to the UIView You could easily add more buttons if you desire to have more than 2.
In the viewDidLoad method: self.navigationController.navigationBarHidden = YES;
In viewDidAppear method: [self.navigationController setNavigationBarHidden:YES animated:YES];
Create a custom UILabel, pin the leading, top, and trailing to the superView, and set the desired height constraint.
Create a custom UIViewwith a height constraint of 1 (to act as the visual separator between the custom Navigation Bar and UITableView, pin the trailing and leading edges to the superView, and pin the bottom space of the UIView to the bottom space of the custom UILabel. Set the desired color for the separator.
Create two UIButtons, drag them into the label, and set these constraints:
-pin the UIButton center Y's to the center Y of the UILabel
-pin Button1's trailing to Button2's leading edge to each other and to the horizontal center of the superview
-Pin Button 1's leading and Button2's trailing to the superView
-Pin the top of the UITableView to the bottom of the UILabel
Set the UIButton color to change when selected, and set their targets as you would with any UIButton
Add any missing constraints in case I forgot to mention some.
Here is what it looks like:
Related
In StoryBoard, if we were using UINavigationController, the embedded UIViewController, will automatically come with a title bar, with horizontal separator.
Using UINavigationController in StoryBoard
Horizontal separator (Circled in red)
If we are using XIB, it is not possible to place UINavigationController in XIB. Hence, in order to have title bar. We need to place it manually.
Added title bar (navigation bar) manually in XIB
However, with such approach, I am not getting the horizontal separator as shown below.
No horizontal separator
May I know if I were using XIB, how can I have a horizontal separator below title bar, without using UINavigationController?
Change the order of the views in the view hierarchy...
Here's how it looks in the order you've setup:
output:
If we move the Navigation Bar to the bottom of the list of subviews - thus bringing it to the Top of the view hierarchy:
It now looks like this:
Actually , NavigationBar has already a horizontal seperator but you couldn't see because of your collectionview 'under the navigationbar' top constraints might be zero. That cause collectionview covers the horizontal seperator.
When you use NavigationControllerall the componens in viewcontroller settle down under the navigationBar but if you use like you posted , They all in the same line . Thats why giving zero top constraints make causes the cover horizontal seperator.
You can change the top constraints of collectioview and you going to see the seperator.
I have an image for middle UITabBarItem which should start from the bottom and align with existing UITabBar. How can I change the height of UITabBarItem for middle item?
To achieve this you need to use your custom TabBarView. It is a simple view, that pinned to the bottom. Design like you want and that's all! This is the best way without issues with working default TabBar.
every "not default looking" tab bar - it is custom
I've created 4 subviews of white color & a yellow one as you can see in reference image I've shared below.
And I've programmatically changed the position of Tabbar from bottom to top just below to navigation bar as you can see in below image (When it is running in the simulator).
Now since I've constraints for that yellow view in image as follow
It is appearing just below to navigation bar but I want it to be displayed just below the Tabbar.
Since Tabbar is getting its position programmatically & other views (including yellow view) are getting their positions from the storyboard.
And since storyboard UIelements are get settled before any other UIelement which are coming from the program or at least get their position from programmatically.
What could be the best way to achieve what I want.
Please refer my storyboard as well to get more understanding. (Refer Below image)
I also want to fit all 5 subviews in the space between tabbar & bottom of the screen. I want to calculate 1/5th of that space & assign this height to each subview. I'd later reduce few pixels to separate them.
Why not add the height of the tab bar to the yellow view's top position? That way, you'd set the constraint something like:
Fajar.top = top + 44
If the tab bar is always visible, then that should work. But of course, if the tab bar only appears at times, you'd probably have to change that constraint programmatically depending on the change ...
I'm trying to make an app with a toolbar that can be resized. Basically, the toolbar can alternate between being at the bottom of the view and being at the top. When a button is pressed, it switches from one to the other. The problem is that when it is at the top, I want the size of the toolbar to expand to accommodate the status bar, but I don't know how to do this.
I've seen some solutions for changing the toolbar size but they all seem static and not something that can be changed with the tap of a button. Any suggestions on how to do this? Perhaps a different solution altogether?
You can use a normal UIView and customize it so it looks like a UIToolbar, then just set constraints using AutoLayout and animate the height-constraint.
I need to do exactly what the photos app is doing when you press the select button. Basically just hides the UITabBar and presents a UIToolbar. For some reason this seems to be incredibly difficult if you don't want to implement a complete hack. I found a hack if you shrink the height of the UITabBar and change it's alpha to 0 but when you set it's height back to the default the image and text is condensed.
Turns out that I ended up just needing to call [self.tabBarController.tabBar setHidden:YES] to hide the tabBar and instead of using my existing UINavigationController's toolbar I create my own instance of a UIToolbar and add it as a subview of my view controller. Then using autolayout I pinned it to the leading, trailing, and bottom edge of the view. This handles rotation and other issues.