Prevent a UISearchDisplayController from hiding the navigation bar

后端 未结 15 1451
南方客
南方客 2020-11-27 11:59

Whenever a user begins editing a UISearchDisplayController\'s search bar, the search controller becomes active and hides the view\'s navigation bar while presen

相关标签:
15条回答
  • 2020-11-27 12:15

    Since iOS 8.0 the same behavior can be achieved by setting the UISearchController's self.searchController.hidesNavigationBarDuringPresentation property to false.

    The code in Swift looks like this:

    searchController.hidesNavigationBarDuringPresentation = false
    
    0 讨论(0)
  • 2020-11-27 12:15

    As jrc pointed out "unhook UISearchDisplayController from controlling any UISearchBar" seems to work for me. If I pass nil as a parameter when creating UISearchDisplayController the navigation bar stays visible at all times:

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:nil contentsController:self];
    
    0 讨论(0)
  • 2020-11-27 12:16

    Really nice solution, but it was crashing my app under iOS6. I had to make the following modification to get it work.

    @implementation ICSearchDisplayController
    
        - (void)setActive:(BOOL)visible animated:(BOOL)animated
        {
            if (visible == YES) {
                [super setActive:visible animated:animated];
                [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
            } else {
                [super setActive:NO animated:NO];
            }
        }
    
    0 讨论(0)
  • 2020-11-27 12:19

    @Pavel's works perfectly well. However, I was trying to get this into a UIPopoverController and the text in the field gets pushed slightly when the search bar's text field becomes the first responder, and that looks a bit ugly, so I fixed it by calling the super method with animated set to NO.

    0 讨论(0)
  • 2020-11-27 12:21

    I was adding custom navigation bar on my ViewController which was getting hidden on search, a quick but not so good fix was

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
        [self.view addSubview:_navBar];
    }
    

    _navBar is UINavigationBar added programmatically, doing this helped me navigation bar from hiding.

    0 讨论(0)
  • 2020-11-27 12:24

    The new UISearchController class introduced with iOS 8 has a property hidesNavigationBarDuringPresentation which you can set to false if you want to keep the navigation bar visible (by default it will still be hidden).

    0 讨论(0)
提交回复
热议问题