UISearchController Not Redisplaying Navigation Bar on Rotate

后端 未结 5 1928
醉话见心
醉话见心 2020-12-08 23:10

I have implemented the UISearchController and it is working great except...

When I click on the Search Bar the Navigation Bar disappears nicely as expected. When I r

5条回答
  •  误落风尘
    2020-12-08 23:49

    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);
        }
    }
    

提交回复
热议问题