2015-02-11 26 views
1
透明UIStatusBar

我使用以下代碼來顯示一個UISearchBar與UISearchController

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

嘗試'self.automaticallyAdjustsScrollViewInsets = YES; ' – orkenstein 2015-02-11 08:01:26

+0

它不適用於這個 – sger 2015-02-11 08:05:38

+0

「紅色列表」是一個部分標題? – orkenstein 2015-02-11 08:12:40

回答

0

也許你可以隱藏搜索過程中的狀態欄。一個自定義子類應該可以工作:

class MySearchController: UISearchController { 
    override func prefersStatusBarHidden() -> Bool { 
     return true 
    } 
} 
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; 
}