UISearchBar animation issue

瘦欲@ 提交于 2019-11-28 05:56:35

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.

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.

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..

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];
     }
}
smitt04

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.

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

self.edgesForExtendedLayout = UIRectEdgeNone;

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 )

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;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!