簡體   English   中英

從 UISearchController 搜索欄 swift 中刪除黑色邊框

[英]remove black border from UISearchController search bar swift

我的 tableView 中有一個 UISearchController。 另請注意,頂部有一個導航項。 我的問題是我加載頁面時頂部和底部有一個黑色邊框,但是當我單擊搜索欄時它不存在。

頁面加載時的搜索欄(帶黑色邊框):

在此處輸入圖片說明

單擊搜索欄后(無黑色邊框):

在此處輸入圖片說明

這是相關的代碼:

let searchController = UISearchController(searchResultsController: nil)

在視圖DidLoad中:

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

沒有一個工作。 這是我使用代碼的順序的問題,還是我將如何擺脫這些行?

經過大量搜索修復了這個問題。 這是我如何做到的。 viewDidLoad ,添加了以下幾行:

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

之后在didFinishLaunchingWithOptions中的 app.delegate 文件中添加了以下代碼:

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

(感謝諾埃爾從這個答案中獲得的擴展)

最后,想要的結果:

在此處輸入圖片說明

我認為這是您需要刪除邊框的導航欄。

如何隱藏 iOS7 UINavigationBar 1px 底線

適用於 iOS 13 / Swift 5; 以下行是訣竅:

  UISearchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM