3

i'm using the following code to display a UISearchBar

    searchController = UISearchController(searchResultsController: resultsTableViewController)
    searchController?.searchResultsUpdater = self
    searchController?.searchBar.sizeToFit()
    searchController?.searchBar.backgroundColor = UIColor.whiteColor()
    searchController?.searchBar.searchBarStyle = UISearchBarStyle.Minimal
    self.tableView?.tableHeaderView = searchController?.searchBar
    searchController?.delegate = self
    searchController?.dimsBackgroundDuringPresentation = false
    searchController?.searchBar.delegate = self

    definesPresentationContext = true

my problem is when i'm entering search mode the view goes fullscreen and i can see the content of tableview overlaps with UISearchBar is this a bug any solution to this problem?

see screenshot

enter image description here

my solution

   func willPresentSearchController(searchController: UISearchController) {
    topBarView = UIView(frame: CGRectMake(0.0, 0.0, self.view.frame.size.width, 20.0))
    topBarView?.backgroundColor = UIColor.whiteColor()
    AppDelegate.sharedAppDelegate().window?.rootViewController?.view.addSubview(topBarView!)
}

func willDismissSearchController(searchController: UISearchController) {
    topBarView?.removeFromSuperview()
}
sger
  • 719
  • 2
  • 12
  • 26

2 Answers2

0

Perhaps you could hide the status bar during search. A custom subclass should work:

class MySearchController: UISearchController {
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}
EPage_Ed
  • 1,173
  • 11
  • 11
0

You don't need to do that hack with search controller methods. Rather, you can edit search bar properties:

self.iSearchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
self.iSearchController.searchBar.backgroundImage = [UIImage imageWithImage: [UIImage imageNamed:@"whiteBackgroundImage.png"] withTintColor:[UIColor colorWithRed:246/255.0f green:246/255.0f blue:246/255.0f alpha:1.0f]]; // or you can just create any random plain image with this color and use it with imageNamed: method.
    self.iSearchController.searchBar.barTintColor = [UIColor colorWithRed:246/255.0f green:246/255.0f blue:246/255.0f alpha:1.0f];

For your convenience imageWithImage:withTintColorMethod is defined below:

@implementation UIImage (Category)

+ (UIImage *) imageWithImage: (UIImage*) image withTintColor: (UIColor*) color {
    UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIGraphicsBeginImageContextWithOptions(newImage.size, NO, newImage.scale);
    [color set];
    [newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
coolcool1994
  • 3,704
  • 4
  • 39
  • 43