Embedding a Collection View inside a Container View - extra white-space at top - ios

I have an app that uses a Nav Contoller as it's initial VC, which then has a root UIViewContoller that contains a UIView at the top half, and a UIContainerView at the bottom. In the UIContanerView, I'm embedding a working UICollectionView that contains image buttons that segue to detail views.
The problem is that white space now shows up at the top of the UICollectionView. Given this is around 64 pixels high, it appears to be a ghosting of a Nav Bar 44px + Status Bar 20px = 64.
And if I scroll up everything looks fine and works as expected, and it also allows me to show you what I expected the layout to look like upon launch:
A snippet of my storyboard is below if that helps:

yes, that could be because child view controller embedded in container view gets the impression, that it is a direct child of UINavigationController, which in turn make collectionView leave top 64 pt insets.
TO solve this problem,In your child view controller interface builder, unmark adjust scrollView insets
This should solve your problem
UPDATE
As Dan suggested, we can also fix it programatically, by calling
automaticallyAdjustsScrollViewInsets = false
in viewDidLoad() of your UIViewController

Related

UIView inside UIScrollerView displays with -64 offset

I'm trying to create a form that will be larger than the screen size and I've decided to use a view inside a scrollerview, then I'm adding the rest of the UI widgets (textfields, buttons, etc.) inside the view.
The view displays and scrolls, but no matter what I try, it displays with a vertical offset of -64 below the navigation controller bar. Once there the view will happily scroll up down.
I want the view to anchored at the top, just below the navigation bar, and then scroll down.
I am using IB.
Lots of time researching and not many answers that have worked for me. UIScrollView seems to be like one of those dirty secrets the whole iOS world seems to avoid....
Thank you for your help.
You can fix this in the storyboard
1- Select the view Controller
2- GO to attributes inspector
3- Uncheck adjust scroll View insets
For your view controller, in the storyboard uncheck the option to extend edges under top bar.

ViewController - main view elements at (0,0) are obscured by navigation bar

I have a simple case where I am pushing a new viewcontroller onto a navigation controller stack. This VC uses a xib and has a UISearchBar that is constrained to the top and left 'container' which in this case is simply the main View.
When I push this VC on the stack the search bar is not seen. This is because the nav bar rests on top taking up the first 50 or so y points of the device screen. (starting after the status bar presumably).
If I set the top container auto-layout constraint to 50, or some y-offset sufficient to counteract the height of the nav bar, I will see the search bar.
I am working on a project where I am pushing VC's onto a nav stack of a UINavigationController I did not create. I am wondering what reasons could lead to this (what i would call buggy) behavior. My understanding was that any viewController in a navController would have it's main view's frame adjust in the presence of nav/status/tab bars. What can I do to ensure that origin (0,0) starts at screen origin + status bar height + nav bar height? Furthermore, I would also like my VC's mainView's lower bound to end at the top of the tab bar also present on screen. I have looked into various UIView and ViewController properties and have not found a satisfying solution.
If you use xib file for designing your view controller, you should add this code to your viewDidLoad method:
self.edgesForExtendedLayout = UIRectEdgeNone
This line will make your view controller's view is laid out under the navigation bar.
Conventionally, any new UIViewControllers are done on storyboards and some developers hook up segues between the first and second UIViewController. The reason for this is because by implementing a pushing segue, the secondUIViewController over the first one, the second one will inherit whatever properties the first one had (such as navigation bars, toolbar, tabbar) and it shows on the storyboard immediately.
So then it will be easier to work on the second UIViewController knowing how much space to allocate to UINavigation or UITabbar.
If your VC is done on an XIB interface instead of being hooked to the UINavigationController controlled stack of previous ViewControllers, then I am guessing you are not seeing the space that UINavigationBar and UITabBar supposedly takes up on your interface?
Perhaps you could try setting the constraints to begin at 64 points below the Top Layout Guide (to make space for UINavigationBar) and 49 above the Bottom Layout Guide (to make space UITabbar).

iOS UIPageViewController child view under Bottom Bar

I had an UIPageViewController embedded in a NavigationController embedded an TabBarController.
I supposed every child view of the UIPageViewController fits the size within the UITabBarViewController.
The first child view looks fine:
Switch to the next (vertically), it's view suddenly resizes and the view length expands over the bottom bar:
Actually it's not under the bottom bar but clipped to that size (which means if you pull up the view you still cannot see the whole but the cut text).
I did unchecked every related view controller's Under Bottom Bar & Adjust Scroll View Inset but nothing works.
Any suggestion would be appreciated.
Try this in table view controller viewDidLoad() method.
self.extendedLayoutIncludesOpaqueBars = NO;
OR
you can set property in Interface Builder:
Uncheck Extend Edges: Under Bottom Bars, Under Opaque Bars.

UIViewController auto layout in iOS8

After updating device to iOS 8 the next issues with layout occur.
I have an UITabBarController which contains two UITableViewController embedded in UINavigationController. This is main view.
Also there are several UIViewControllers designed in StoryBoard (not embedded in UINavigationViewControllers). These are secondary views. All of them have "Hide Bottom bar on Push" set to YES. Constraints are set to determine layout.
All secondary views are shown with:
UIStoryboard* sb = [UIStoryboard storyboardWithName:STORYBOARD_NAME bundle:nil];
UIViewController* secondaryView = [sb instantiateViewControllerWithIdentifier:_name];
[navigationController pushViewController:secondaryView animated:YES];
All works fine on iOS7.
But with iOS8 there are some problems when secondary view controller appears:
All pinned to bottom UIViews are shown on wrong places for a moment and then "jump" to correct place. Looks like first position is calculated including bottom bar (which is hidden due to "Hide on Push")
If there is UITableView as subview: top pinned subviews again are show on wrong place for a moment and then "jump" to correct place. Looks like first position is calculated not including navigation bar size. If i remove UITableView from controller - all works fine (except p.1)
Tried to set Simulated metrics for secondary views - did not help.
These "jumps" are really annoying. Would appreciate for any advices.
I had a similar problem with jumping when hiding the tab bar from the Storyboard.
Make sure your bottom subviews are pinned to the superview rather than the Bottom Layout Constraint.
You have to do this from the menu - Editor - Pin - Bottom Space to Superview, because the Auto Layout menu in Interface Builder pins to Bottom Layout Constraint by default.
As far as i experienced iOS 8 have issue in tabbar controller mixed with navigation bar controller .. removing one of them will make it run ok again.

UISearchBar on UITableView strange offset issue

I have a UITableView which has a UISearchBar subview. This is all on the view of a UIViewController along with a few other subviews (labels, text fields and such).
The search bar and content offset of the table are acting quite strangely, but it seems dependent on the order in which these views are added to the main view in the xib. I created a sample project with just my table/search and a label in order to test, and the result is the same. When the table is added after the label, everything works fine:
Setup:
Correct and Expected Result:
However, if I simply change the order in which my 2 subviews sit on the main view (aka table added before the label) then weird things start happening.
Apparently bad setup:
Weird offset of Search Bar:
I'm not changing anything else whatsoever, so why does Xcode seem to care which order these subviews are added to the main view?? If I scroll up on the "bad" table setup, the search bar disappears immediately at its top edge, but the table's content will continue to scroll up until it reaches the top of the frame that was set in the xib. Scroll back down and the search bar doesn't reappear until the strange lowered location. This is in Xcode 5.1.1, not the new beta. The result is the same with or without Autolayout turned on.
Any idea why this is happening? Is this a bug, or am I missing something? (I didn't post any code because all I'm doing is setting the number of sections, rows, and setting the text on the cell. Not messing with content insets, offset, anything. I load the view from the app delegate as the root of a nav controller)
This happens because a UIViewController's property called automaticallyAdjustsScrollViewInsets
With iOS 7, UIViewControllers have a property called
automaticallyAdjustsScrollViewInsets, and it defaults to YES. If you
have a scroll view that is either the root view of your view
controller (such as with a UITableViewController) or the subview at
index 0, then that property will adjust both the contentInset and the
scrollIndicatorInsets. This will allow your scroll view to start its
content and scroll indicators below the navigation bar (if your view
controller is in a navigation controller).
From Big Nerd Ranch
If you are using storyboards, you can change it by selecting the view controller and in the attributes inspector deselect Adjust scroll view insets.
Here is its description from apple documentation:
Default value is YES, which allows the view controller to adjust its
scroll view insets in response to the screen areas consumed by the
status bar, navigation bar, and toolbar or tab bar. Set to NO if you
want to manage scroll view inset adjustments yourself, such as when
there is more than one scroll view in the view hierarchy.
I have same problem before about position of tableview and searchbar. i tried the following and it works for me.
If you do not write code for that and if it is only problem of xib or storyboard then try all outlet's autosizing and origin setting to fix its position and see the difference. it may be work for you.
Update : automaticallyAdjustsScrollViewInsets has been deprecated in ios 11 and a new field contentInsetAdjustmentBehavior has been introduced.
if #available(iOS 11.0, *) {
tableview.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
UITableView header can contains only one UIView, so if you need UISearchBar plus UILabel, you need to wrap they into UIView and add this view as UITableView header.

Resources