ios - 导航栏下方添加 "static"搜索栏

标签 ios swift swift3 uinavigationbar uisearchbar

我进行了大量搜索并阅读了很多问题/答案,但我仍然无法在导航栏下方编写静态搜索栏......(换句话说,搜索栏固定在导航栏上)

我想要这样的东西:

enter image description here

但是我不知道怎么做。

这是我写的代码:

let searchBar = UISearchBar()
      searchBar.placeholder = "Search"
      searchBar.frame = CGRect(x: 0, y: 0, width: (navigationController?.view.bounds.size.width)!, height: 64)
      searchBar.barStyle = .default
      searchBar.isTranslucent = false
      searchBar.barTintColor = UIColor.red
      searchBar.backgroundImage = UIImage()
      view.addSubview(searchBar)

当我向上滚动时,搜索栏不会粘在导航栏上。我想为这两个部分准备一个积木。

最佳答案

enter image description here

按照下面的步骤在UINavigationBar下实现一个静态的UISearchBar

  1. UISearchBar 从 Interface Builder 拖到 Storyboard 中的 ViewController。

  2. UISearchBar 设置约束

    • 置顶
    • 领导
    • 尾随
    • 固定高度

关于ios - 导航栏下方添加 "static"搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44024288/

相关文章:

ios - 如果连接了硬件键盘,则隐藏 inputAccessoryView

objective-c - 将单词列表字典加载到数组中的最快方法?

ios - 将数据从单元格加载到 detailView 始终显示最后的数据

ios - 分享到 Messenger 的链接时缺少报价

swift - 无法在 swift 3.0 中转换正确的设备 token

android - 移动 Web 开发框架

ios - Swift 2 - 使用 UIAlertController 将 3 个 Action 按钮放在同一行

swift - 当我读取属性时调用 WillSet

swift - 如何正确地将 sqlite 框架添加到 Xcode 项目中?

json - Swift 3 - 提取 JSON 数据