19

I have a UIViewController in which I want to show a tableview with the serchBar.

//viewDidLoad
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                           0, 
                                                           SCREEN_WIDTH(),
                                                           SCREEN_HEIGHT())
                                                    style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;

[self.view addSubview:_tableView];

// adding uisearch bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

_tableView.tableHeaderView = searchBar;

//
 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;

The issue happens when I click inside the uisearch bar so that the animation starts and it looks like it has a 20px unwanted offset.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
luca
  • 36,606
  • 27
  • 86
  • 125

8 Answers8

15

In your Storyboard, select the problematic controller, look at the Attributes tab and try to edit these settings:

  • Under Top Bars
  • Under Opaque Bars

I've solved a similar problem by unflagging these settings.

Firegab
  • 313
  • 1
  • 9
8

I found what is causing this issue. Seems that the animation gets messed up when you set navigationBar.translucent to NO. If you make your navigationBar translucent, everything should work fine, but this is definitely not an ideal solution. I'm going to try and find a workaround.

codyko
  • 654
  • 6
  • 10
1
UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                           64,
                                                           self.view.frame.size.width,
                                                           self.view.frame.size.height)
                                          style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;

[self.view addSubview:_tableView];

// adding uisearch bar
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

_tableView.tableHeaderView = searchBar;



//
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;

and I just embade my controller with UINavigationcontroller and its working quite well..enter image description here

Retro
  • 3,985
  • 2
  • 17
  • 41
1

You can cancel animation by subclassing UISearchDisplayController and adding this:

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
     if(self.active == visible) return;
     [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
     [super setActive:visible animated:animated];
     [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
     if (visible) {
          [self.searchBar becomeFirstResponder];
     } else {
          [self.searchBar resignFirstResponder];
     }
}
Irina
  • 409
  • 5
  • 17
1

codyko gave me an idea. It was the because the navigation bar was no translucent. So I set it to translucent on this view controller and rest when I left with the following code:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

Now that left the navigation bar on this controller slightly faded, so I added a UIView the same color as my navbar just behind it to make it look opaque. I know it isn't perfect but it works nicely.

Community
  • 1
  • 1
smitt04
  • 3,018
  • 2
  • 28
  • 30
1

As a reminder for anyone with similar issues. I needed to add this line that fixed things:

self.edgesForExtendedLayout = UIRectEdgeNone;
luca
  • 36,606
  • 27
  • 86
  • 125
0

Why are you creating the searchBar programmatically instead of in the StoryBoard ? I'm currently using searchBar, added in storyboard and it's work fine ( I have to change the contentOffset )

CZ54
  • 5,488
  • 1
  • 24
  • 39
  • What do you mean for changing the content offset? can you post a snippet of your code? – luca Oct 26 '13 at 11:35
  • Yep, there is it : `-(void) viewDidLayoutSubviews{ [self.tableView setContentOffset:CGPointMake(0, -60)]; }` – CZ54 Oct 29 '13 at 10:55
-2

I have applied your code,, It works fine for me,, Just hide you navigation bar and start the search bar from y = 20, instead of y = 0;

Sundeep Saluja
  • 1,089
  • 2
  • 14
  • 36