使用UISearchController创建透明的UIStatusBar

3
我使用以下代码来显示一个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重叠在一起,这是一个bug吗?有没有解决这个问题的方法?
请查看截图。
我的解决方案:
   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()
}

尝试使用 self.automaticallyAdjustsScrollViewInsets = YES; - orkenstein
它与此不兼容。 - sger
“红色列表”是一个章节标题吗? - orkenstein
不要我的简单解决方案以上。 - sger
你以一种相当粗糙的方式操作子视图。你想要实现什么? - orkenstein
显示剩余2条评论
2个回答

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

0
也许你可以在搜索期间隐藏状态栏。一个自定义的子类应该可以解决:
class MySearchController: UISearchController {
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接