2017-10-06 148 views
0

之前已經詢問過這個問題的各種變化,但沒有一個解決方案適用於我。我已經使tableViewHeader searchBar,這是我迄今爲止。帶邊框的自定義搜索欄

我的代碼如下所示:

override func viewDidLoad() { 
    super.viewDidLoad() 
    configureSearchBar() 
    self.tableView.separatorColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0) 
    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem 
} 

func configureSearchBar() { 
    searchController.searchBar.delegate = self 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.hidesNavigationBarDuringPresentation = false 
    searchController.searchBar.placeholder = "Search Companies" 
    definesPresentationContext = true 
    tableView.tableHeaderView = searchController.searchBar 
    searchController.searchBar.barTintColor = UIColor.white 

    //makes border for search box and gives color and rounded 
    //searchController.searchBar.layer.borderColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0).cgColor 
    //searchController.searchBar.layer.borderWidth = 2.0 
    //searchController.searchBar.layer.cornerRadius = 15.0 

} 

而且我試圖建立這一點,但到目前爲止,我還沒有成功

What I am trying to build

如果我試試這個:

searchController.searchBar.layer.borderColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0).cgColor 
    searchController.searchBar.layer.borderWidth = 1.0 
    searchController.searchBar.layer.cornerRadius = 15.0 
    searchController.searchBar.clipsToBounds = true 

它最終看起來像這正是我正在尋找。

What it ends up looking like

謝謝您的幫助先進

+0

如果您發佈當前代碼,則更有可能獲得有用的回覆。 – nathan

+0

[如何更改搜索欄(Swift)中的邊框和圖標]的可能重複](https://stackoverflow.com/questions/31413127/how-to-change-border-and-icons-in-search-bar-swift ) – arvidurs

回答

0

我能夠讓它像這樣工作。而不是改變標題的大小需要改變的是搜索欄中文本框的大小。

func configureSearchBarTextField() { 
    for subView in searchController.searchBar.subviews { 
     for subsubView in subView.subviews { 
      if let textField = subsubView as? UITextField { 
       var bounds: CGRect 
       bounds = textField.frame 
       bounds.size.height = 35 //(set height whatever you want) 
       textField.bounds = bounds 
       textField.layer.cornerRadius = 5 
       textField.layer.borderWidth = 1.0 
       textField.layer.borderColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0).cgColor 
       textField.backgroundColor = UIColor.white 
      } 
     } 
    } 
} 
0

這對我的作品

self.searchBar.layer.borderColor = UIColor.cyan.cgColor 
self.searchBar.layer.borderWidth = 1 
self.searchBar.layer.cornerRadius = 5.0 
self.searchBar.clipsToBounds = true 

,並在委託更新的高度頭:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 200 
} 
+0

這給了搜索欄帶有圓角的邊框,但它與其中一個單元保持相同的大小,並且頂部和底部邊框被裁剪到單元的頂部和底部。有沒有辦法改變可能給我我想要的高度? –

+0

然後只需更改標題的高度。 – arvidurs

+0

我一直在試圖做到這一點,但我無法弄清楚。我是初學者,我該怎麼做? –