UITableView content overlaps Status Bar when UISearchBar is active

后端 未结 8 621
北海茫月
北海茫月 2021-02-05 02:17

I have a UITableViewController with a UISearchBar and UISearchDisplayController. That exists inside a Container View in a UIViewController which is in a UINavigationController.

相关标签:
8条回答
  • 2021-02-05 02:50

    Here's what worked for me:

    DO:

    • Use UISearchController (not a separately placed UISearchBar)
    • Place your VC in a UINavigationController if it isn't already. Set the nav not to "Show Navigation Bar" if desired.
    • Use autolayout for the UITableView (not springs and struts) and pin the top of the table to the top of the VC's view.
    • Add this delegate method:

    - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { return UIBarPositionTopAttached; }

    DON'T:

    • Fiddle with edgesForExtendedLayout
    • Fiddle with extendedLayoutIncludesOpaqueBars
    • Fiddle with the table's contentInset
    0 讨论(0)
  • 2021-02-05 02:51

    I had the same problem:

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        controller.searchBar.searchBarStyle = UISearchBarStyleDefault; // Used to cover UIStatusBar
    }
    
    - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
    {
        controller.searchBar.searchBarStyle = UISearchBarStyleMinimal; // Used not to show top and bottom separator lines
    }
    
    0 讨论(0)
  • 2021-02-05 03:00

    I have UISearchBar and UISearchDisplayController.

    In viewdidload:

    self.edgesForExtendedLayout = UIRectEdgeNone;
    [searchDisplayController.searchBar setBackgroundImage:[self imageWithColor:ETSBaseColor] forBarPosition:0 barMetrics:UIBarMetricsDefault];
    

    method that obtain image from UIColor:

    - (UIImage *)imageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    
    0 讨论(0)
  • 2021-02-05 03:03

    I have search this for hours and my final result was to put this line in viewDidLoad: self.extendedLayoutIncludesOpaqueBars = YES;

    Problem solved :)

    0 讨论(0)
  • 2021-02-05 03:08

    In my case I don't want to hide the UINavigationBar but I had similar problems with gapes and other side effects. One of them was a missing UISearchBar after switching between UIViewControllers while the UISearchDisplayController is visible (I'm using SWRevealViewController to switch between UIViewController). This problem occurs only on iPads. It came out that the UISearchBar suddenly hides behind the UINavigationBar. Now I solved all my Problems with the following lines of code in the UITableViewController which is presented in a UIContainerView:

    - (UINavigationController *)navigationController {
        return nil;
    }
    

    Those lines prevent the UISearchDisplayController to reach and change my UINavigationController. I also subclassed this method into "MyContainerTableViewController" class and use this class now for all embedded UITableViewController.

    I'm still using UISearchDisplayController to Support iOS 7.

    0 讨论(0)
  • 2021-02-05 03:09

    The following hack worked for me:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return (self.searchController.isActive  && section == 0) ? 22.0f : 0.0f;
    }
    
    0 讨论(0)
提交回复
热议问题