带边框的自定义搜索栏

问题描述:

之前已经询问过这个问题的各种变化,但没有一个解决方案适用于我。我已经使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

我能够让它像这样工作。而不是改变标题的大小需要改变的是搜索栏中文本框的大小。

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

这对我的作品

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

我一直在试图做到这一点,但我无法弄清楚。我是初学者,我该怎么做? –