Programmatically add UISegmentedControl in UIToolbar below UINavigationBar - ios

I'm trying to get this to work on iOS 7 and 8+.
In loadView of the viewController, I add a UISegmentedControl, along with flexible spaces, as items to a UIToolBar. I then add the UIToolbar as a subview to the main view, setting the vertical position to be the height of the navigation bar.
First problem. The UISegmentedControl is vertically off center so the top of it, is cut off.
Second problem. Rotating to landscape messes it all up. Specifcally, the UIToolbar seems to move underneath the navigation bar whereas the UISegmentedControl doesn't.
Autoresizing issue? I've tried various settings and can't seem to get it to center vertically within the UIToolbar.

It could be that your UIToolbar constraints are not set properly and also that there is no flexible space around the segmented control. Here's a link to a storyboard file I made that have proper constraints and flexible space, tested to work.
Preview:
Here's the Storyboard file: http://www.filedropper.com/main_4

Related

UIView overlapping with tab bar elements iOS

I made a view for a switch view. Now I already had tabbed menu buttons in it. The problem is the subview for the switch view seems to be overlapping with the tab bar icons at the bottom. Please help.
i think thats because your tabBar is set to translucent. set it to non-translucent from storyboard or programatically.
self.tabBarController.tabBar.translucent = false
You should have a constraint that sets the vertical space between your subview and your tabbar.
Also, check that your layout respects the iPhone X Safe Area (https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/), from your image it seems that your elements overlap the safe area at the bottom.

UIView Under Nav and Tab Bars

Using storyboard and Swift, I have a view controller which has two elements on it. A UITextField, and a UIView, which I use merely to give a colored border to the UITextField. The UITextField is a child of the UIView. See:
The problem I am having is that despite having set the top bar and bottom bar simulated metrics attributes to Opaque Navigation Bar and Opaque Tab Bar respectively, See:
When the app builds and runs the top of the UIView is always underneath the nav bar. My over all feeling is that this is somehow a constraints issue but I have not been able to find the solution to it. How do I set the constraints so that the UIView is always immediately underneath the nav bar, and the bottom of the view is always just on top of the tab bar?
Select your UIView in storyboard and assign constraints to the top margins and to bottom margins. There is a 'Pin' button you use to do this that looks something like a Tie-Fighter ship in Star Wars, it is located at the bottom right of your storyboard view.
This will set margins to stretch to top and bottom always.
That is what the layout guides are for. Pin the top to the Top Layout Guide and the bottom to the Bottom Layout Guide. The guides will always move to adjust for any top and bottom bars.

Add a UINavigationBar to a UIView using Autolayout

I have a modal view that I'm presenting over the current view that has a UINavigationBar as a subview. Everything's working great, except that the UINavigationBar doesn't want to behave according to the autolayout constraints. Everything else in the view behaves as desired.
For some reason, the left and right bar button items are cut off on the edges of the view. I inspected the view at runtime to reveal that the total width of the UINavigationBar is the right width, but the bar buttons aren't conforming.
I also checked to make sure I didn't have negative margins or some other anomaly in the constraint definitions.
I have also tried setting the frame of the UINavigationBar manually, but it yielded no improvement.
Problematic View
Constraints
I'd like to keep the UINavigationBar, because it gets styled globally with the rest of my app (as opposed to creating a custom UIView imitation and styling it individually). Does anyone know what handle to jiggle to get the bar button items to conform to the right width?

Autolayout UITabBar items in iOS

I am trying to implement a simple mobile browser in iOS. I am trying to include two UIToolbars; one at the top and one at the bottom. The top toolbar will consist of a UITextField and a GO button. The bottom toolbar will consist of back and forward buttons.
I want this to work correctly in both portrait and landscape modes. However, I noticed that autolayout does not work for UIToolbar items. So I cannot set constraints for my UITextField and the buttons. How do I fix this?
Yes autolayout does not work for UIToolbar items because they are mostly UIBarItem instead of subclass of UIView.
You can still set up constraints by using special items

Recreate UITabBar to UIToolbar on select from Photos app

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.

Resources