Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

table added after label

Correct and Expected Result:

Proper 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:

Table added before label

Weird offset of Search Bar:

wtf is this?!?!

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)

like image 276
Stonz2 Avatar asked Jun 20 '14 20:06

Stonz2


2 Answers

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.

like image 163
Mustafa Ahmed Avatar answered Sep 21 '22 12:09

Mustafa Ahmed


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.

enter image description here

like image 26
Max Avatar answered Sep 19 '22 12:09

Max