UISearchController Not Redisplaying Navigation Bar on Rotate

扶醉桌前 提交于 2019-11-28 07:00:47
pbasdf

It looks like the UISearchController forgets to reset the frame of the searchBar when the status bar reappears. I think this is probably a bug in UISearchController; there seem to be a few listed in radar. It seems the searchBar's superview (which is internal to the UISearchController) ends up with the wrong height. This is vexing since the solution therefore involves reaching into the searchController's view hierarchy, which Apple could change... you might want to add a check of the iOS version so it only runs for specified versions.

If you add the code below to your view controller, it will be called when the trait collection changes. It checks to see a) the search controller is active, b) the statusBar is not hidden, and c) the searchBar origin-y is 0, and if so it increases the height of the superview by the height of the statusBar, which moves the searchBar down.

override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
    let app = UIApplication.sharedApplication()
    if searchController!.active && !app.statusBarHidden && searchController?.searchBar.frame.origin.y == 0 {
        if let container = self.searchController?.searchBar.superview {
            container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + app.statusBarFrame.height)
        }
    }
}

Objective C

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {

    [super traitCollectionDidChange: previousTraitCollection];

    if(self.venueSearchController.active && ![UIApplication sharedApplication].statusBarHidden && self.venueSearchController.searchBar.frame.origin.y == 0)
    {
        UIView *container = self.venueSearchController.searchBar.superview;

        container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height);
    }
}

You may need to implement UIBarPositioningDelegate in your view Controller.

self.venueSearchController.searchBar.delegate = self;

The searchBar is looking for a response from it's delegate.

@protocol UISearchBarDelegate <UIBarPositioningDelegate> 

Add the following to your self (I assume it's a ViewController)

#pragma mark - <UIBarPositioningDelegate>

// Make sure NavigationBar is properly top-aligned to Status bar
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    if (bar == self.venueSearchController.searchBar) {
        return UIBarPositionTopAttached;
    }
    else { // Handle other cases
        return UIBarPositionAny;
    }
}
Rory McKinnel

I believe this may be an issue with the way UITableViewController layout deals with the navigation bar going as opposed to there being no navigation bar when it appears in the case of you rotating back.

I think you can solve this issue by replacing your UITableViewController with a UIViewController into which you drop a UITableView. Then set the top constraint of the table view to be the top layout guide. Set the side constraints to left, right, bottom of 0.

This should ensure that the table always stays below the status bar and will still move correctly when the nav bar goes and comes back.

See this discussion: iOS 7: UITableView shows under status bar

I've faced a similar issue with UISearchController using it with navigationItem. It looks differently, but caused by the very same problem: UISearchController does NOT update searchBar frame when status bar reappears.

But instead of manually adjusting frames of searchBar's view hierarchy you should simply force it to update layout by toggling hidesNavigationBarDuringPresentation back and forth during rotation either in traitCollectionDidChange or viewWillTransition(to size: CGSize, with cooridnator: UIViewControllerTransitionCoordinator):

So the fix would look like this:

 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    if #available(iOS 11, *) {
        navigationItem.hidesSearchBarWhenScrolling.toggle()
        navigationItem.hidesSearchBarWhenScrolling.toggle()
    }
}

And here is how the original issue looks like.

Then rotating and got this:

I've "fixed" this temporarily by turning off "Hide status bar" in settings.

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