2016-07-31 71 views
1

我在我的tableView中有一个UISearchController。另请注意,顶部有一个导航项目。 我的问题是,当我加载页面时,我在顶部和底部有一个黑色边框,但是,当我单击搜索栏时它不存在。在页面加载从UISearchController搜索栏中删除黑色边框swift

搜索栏(带黑色边框):

enter image description here

点击搜索栏(无黑边)后:

enter image description here

下面是相关的代码:

let searchController = UISearchController(searchResultsController: nil) 

在viewDidLoad中:

searchController.searchBar.barTintColor = UIColor.redColor() 
     searchController.searchBar.tintColor = UIColor.whiteColor() 

我跟着几个类似的问题,并提出在viewDidLoad()上面的行之后的以下变化:

1)searchController.searchBar.backgroundImage = UIImage()

2)searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

3)searchController.searchBar.layer.borderColor = UIColor.clearColor().CGColor

4)

searchBar.layer.borderWidth = 1 
    searchBar.layer.borderColor = UIColor.whiteColor().CGColor 

无效。这是我使用代码的顺序的问题,或者我将如何摆脱这些行?

回答

2

很多搜​​索后解决了这个。这是我做到的。在viewDidLoad,添加以下行:

self.searchController.searchBar.translucent = false 
    self.searchController.searchBar.backgroundImage = UIImage() 
    self.searchController.searchBar.barTintColor = UIColor.redColor() 
    self.searchController.searchBar.tintColor = UIColor.whiteColor() 

后,在app.delegate文件中didFinishLaunchingWithOptions添加以下代码:

let backgroundColor = UIColor.redColor() 
     let foregroundColor = UIColor.whiteColor() 


     UIApplication.sharedApplication().statusBarStyle = .LightContent 

     UINavigationBar.appearance().shadowImage = UIImage() 

     UINavigationBar.appearance().setBackgroundImage(backgroundColor.toImage(), forBarMetrics: UIBarMetrics.Default) 

     UINavigationBar.appearance().tintColor = foregroundColor 

     UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: foregroundColor] 

还需要在外面的app.delegate文件(这个地方扩展类)

extension UIColor{ 
    func toImage() -> UIImage { 
     let rect = CGRectMake(0, 0, 1, 1) 
     UIGraphicsBeginImageContextWithOptions(rect.size, true, 0) 
     self.setFill() 
     UIRectFill(rect) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

(感谢诺尔从this answer采取的扩展名)

最后,期望的结果:

enter image description here

0

我认为这是您需要删除边框的导航栏。

How to hide iOS7 UINavigationBar 1px bottom line

+0

您的链接了这样的回答:http://stackoverflow.com/a/24974907/4858605可能是有用的potentionally,虽然我无法测试出来。 –