I have a UISearchBar inside of UIScrollView (well, to be precise an AQGridView). Now the searchBar shall be hidden initially (as you see in the Music app for instance). As UIScrollViews don’t have a header view, I placed it -40 points outside of the UIScrollView’s frame and also set the contentInset to 40 points.
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.f, -40.f, 320.f, 40.f)];
gridView.contentInset = UIEdgeInsetsMake(0.f, 40.f, 0.f, 0.f);
Now from the look and general behavior that pretty much does exactly what I want. The search bar is hidden, but I can scroll up to reveal it.
Now here’s the thing:
When I touch on the searchBar to give it focus and enter some text everything works fine except when the searchBar is not entirely visible. In that case the UIScrollView tries to automatically scroll it to complete visibility. However, it scrolls in the wrong direction.
So when, say, 10 points on top of the search bar are hidden, the scroll view scrolls down instead of up.
This might be a little bit vague a question, but maybe someone can tell my mistake here… The only thread I could find here was this one: uitableview contentInset issue but that also didn’t seem to help my case.
I met alomost the same problem and CodaFi’s solution just cannot solve my problem.
So I kind of doubting this solution as the correct answer.
Here is the description of my situation and problem:
I want to add the UINavigationBar into the ContentInset area and UIWebView in ContentSize area of UIScrollView so that UINavigationBar can go and disappear with the web content outside the top of screen when scrolling down. And also inside the UINavigationBar I added one UITextField and one UISearchBar (yes, just like the Safari). Following the part of setting code(in viewDidLoad: method):
The problem is that when I click the textfield or searchbar, the navigationbar(actually with the whole view) goes down in the screen: textfield by 41 points, and searchbar by 42 points(weird!). So it leaves a whole mysterious white space from 0 to 41(or 42) points on the upside of screen. And another weird thing is that it only happens once! After I scroll the screen by finger, it disappears and never shows up again the second time when I click the textfield or searchbar.
I believe the essence of my problem is just like Dennis’s.
After searching on the web for two days, Unfortunately, I could not find the perfect solution to fix the essence of this problem(actually I am not even quite sure about the essence of the problem…).
But as I experimented and observed, my assumption would be that the 41 points that the textfield moved down could be because of the relative position it is set(-64 + 23 = -41) on the navigationbar. And when it is click by user, the system(iOS) checked its position(although I have already set the contentOffset by -64 in viewDidLoad:) and find it is not in the scope of the user and force the scrollview to scroll down until it shows up(41 points toward downside).
I can not understand the behavior of scrollview if we(developer) set a content inset area and interact with it. There is really few document and referrence talking about it. I wish my assumption was wrong and someone could correct me in the future and help us find the truth.
Last, although I could not find the “real” answer, actually I found a solution to this problem (like a remedy to cure the symptom). Here is the code: