带有UISearchController的透明UIStatusBar

时间:2015-02-11 07:58:41

标签: uisearchbar uistatusbar uisearchcontroller

我正在使用以下代码显示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

我的问题是,当我进入搜索模式时,视图全屏显示,我可以看到tableview与UISearchBar重叠的内容是否是这个问题的任何解决方案的错误?

见截图

enter image description here

我的解决方案

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

2 个答案:

答案 0 :(得分:0)

也许你可以在搜索过程中隐藏状态栏。自定义子类应该起作用:

class MySearchController: UISearchController {
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

答案 1 :(得分:0)

您无需使用搜索控制器方法进行此操作。相反,您可以编辑搜索栏属性:

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];

为方便起见,imageWithImage:withTintColorMethod定义如下:

@implementation UIImage(类别)

+ (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;
}