2

PROBLEM

I want to keep the UISearchBar at the top of my UITableview, but it scrolls away with the list when I scroll down.

I found this post and followed it link

It told me to use this code

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
//    UISearchBar *searchBar = searchDisplayController.searchBar;
searchBar = self.searchDisplayController.searchBar;
CGRect rect = searchBar.frame;
rect.origin.y = MAX(0, scrollView.contentOffset.y);
searchBar.frame = rect;
}

I may have missed something, but this method is called but the UISearchBar stil disappears.

Community
  • 1
  • 1
zach
  • 1,281
  • 7
  • 27
  • 41
  • That should be MIN, and the link is about game center's toolbar, which is locked to the bottom of the navigationBar. You want a bar that scrolls with the scrollview. – CodaFi Mar 01 '12 at 01:57
  • Are you sure that the method is being called and that searchBar isn't nil? – fbernardo Mar 01 '12 at 01:58
  • 1
    Have you tried adding the searchbar outside the scrollview so that it stays top? – Pochi Mar 01 '12 at 02:01
  • @CodaFi Sorry i had it as MIN, i just want the searchbar to stay under the navigation bar when the user scrolls. so they can search no matter where they are in the list and not just at the top – zach Mar 01 '12 at 02:02
  • Put the search bar outside the scrollview as @LuisOscar said then. – fbernardo Mar 01 '12 at 02:03
  • @LuisOscar How do i put it on the outside? Just declare it on the outside? Sorry, i am kind of new to this – zach Mar 01 '12 at 02:06
  • @fbernardo it actually seems like searchBar is nil....any ideas? – zach Mar 01 '12 at 02:11
  • You don't have a searchDisplayController connected in IB? – fbernardo Mar 01 '12 at 02:16
  • You are not doing this programatically are you? What i mean is that, if you just want the searchbar to stay there and the contents to scroll down, all you have to do is in the interface builder on the view of that window to make sure the uisearchbar is not nested inside the scroll view so that it doesnt scroll it down. if you added the searchbar on the interface builder then it cant be nil cuz it is automatically being allocated, you might have the pointer not pointing to this one or the contents might be nil. Like bernardo suggests make sure u r linking it in the IB – Pochi Mar 01 '12 at 02:19
  • @LuisOscar i will answer this because your solution was right. – zach Mar 01 '12 at 02:37
  • @LuisOscar Thanks by the way for you patience. – zach Mar 01 '12 at 02:39

2 Answers2

1

Ok so the solution was A LOT easier than i expected. All i had to do in IB was drag the SearchBar out of the View so it was no longer nested.

zach
  • 1,281
  • 7
  • 27
  • 41
1

There is a document from apple developer forum,named "advanced_scroll_view_techniques.pdf",that will help you.

docs: https://developer.apple.com/devcenter/download.action?path=/wwdc_2011/adc_on_itunes__wwdc11_sessions__pdf/104_advanced_scroll_view_techniques.pdf

sample code: http://developer.apple.com/library/ios/samplecode/StreetScroller/Introduction/Intro.html

carl
  • 318
  • 1
  • 9