【问题标题】:How to set border bottom on UISearchBar in Swift3如何在 Swift3 中的 UISearchBar 上设置边框底部
【发布时间】:2016-10-04 18:54:45
【问题描述】:

我想指定 UISearchBar 的底部边框颜色。我已经看到了很多有助于摆脱边框的答案,或者将边框颜色设置为一个整体,但我只想为底部边框设置边框颜色。这是我的代码:

searchController.searchBar.layer.borderWidth = 1 searchController.searchBar.layer.borderColor = UIColor.white.cgColor

有没有办法为底部边框设置borderColor?

【问题讨论】:

  • 您可以添加一个高度为 1pt 的视图作为 UISearchBar 的子视图。这对你有用。
  • 这对我有用!谢谢@AdeelMiraj
  • 不客气。乐意效劳 :)。我已经用参考代码回答了这个问题。

标签: ios swift uisearchbar border-color


【解决方案1】:

只需像这样向您的文本字段添加一个子视图

let frame = CGRectMake(0, textField.frame.size.height - 1, textField.frame.size.width, 1)
let border = UIView(frame: frame)
border.backgroundColor = UIColor.whiteColor()
textField.addSubview(border)

【讨论】:

    【解决方案2】:

    如果您使用的是低于 3(

    let border = CALayer()
    let width = CGFloat(2.0)
    border.borderColor = UIColor.red.cgColor
    border.frame = CGRect(x: 0, y: searchBar.frame.size.height + width, width:  searchBar.frame.size.width, height: searchBar.frame.size.height)
            
    border.borderWidth = width
    searchBar.layer.addSublayer(border)
    searchBar.layer.masksToBounds = true

    我使用的是 swift 3.0,但在添加此评论之前,我最终测试了此功能,但它对我不起作用。我不知道为什么,但这是在任何元素底部显示边框的正确代码。

    我尝试在搜索栏底部使用 insertinglayout,但它不起作用。

    self.view.layer.insertSublayer(border, below:searchBar.layer)

    我不会告诉你这是一个答案。但这会让您了解如何在特定项目的底部添加布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多