ios - 如何使navigationItem.searchController的searchBar使用屏幕的整个宽度

标签 ios uisearchbar uisearchcontroller uinavigationitem

在 iOS 11 和 12 上,我无法找到让 searchBar 使用整个屏幕宽度的方法。

navigationItem.searchController = searchController

我可以使用以下方法添加更多边距:

    let sb = searchController.searchBar
    let selector = #selector(setter: UIScrollView.contentInset)
    if sb.responds(to: selector) {
       sb.perform(selector, with: UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 35))
    }

如果我尝试将 left 和 right 设置为 0...它只会使用默认布局。

编辑:

越来越近:

    for tf in sb.subviews.first!.subviews where tf is UITextField {      
      tf.translatesAutoresizingMaskIntoConstraints = false
      tf.widthAnchor.constraint(equalToConstant: width).isActive = true
      tf.layoutIfNeeded()
    }

现在我正在尝试找出一种应用约束的方法,以便当文本字段处于事件状态时,它将为取消按钮(UINavigationButton)腾出空间

最佳答案

你试过吗?

        let width = UIScreen.main.bounds.size.width
        searchBar.translatesAutoresizingMaskIntoConstraints = false
        searchBar.widthAnchor.constraint(equalToConstant: width).isActive = true

这里搜索栏宽度将始终等于设备屏幕的整个宽度。

关于ios - 如何使navigationItem.searchController的searchBar使用屏幕的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54519968/

相关文章:

iOS 7 UISearchBar 定制当它是第一响应者

ios - UISearchController 在表头 View 中呈现的 UISearchBar 在事件时动画太远

swift - Firebase - 为什么不搜索在我的应用程序中工作的用户? (当用户输入两个字母时更新搜索 Controller )

ios - ARKit中,ARSKViewDelegate中的平面检测委托(delegate)方法有哪些?

ios - 当键盘出现在 iPad 应用程序中时,在工具栏中滚动 UISearchBar?

ios - 如何通过深层链接将用户导航到所需的 View Controller ,以使用 Firebase 全新安装应用程序

ios - 使用 UIBarButtonItem 将 UISearchBar 添加到 UINavigationBar

iOS 11 刷新控件 + 搜索栏 = 刷新时缺少微调器

ios - 如何在 iOS 上绘制烛台图表?

ios - 架构 armv7 :"_OBJC_CLASS_$_KTKRegion"的 undefined symbol ,引用自 : objc-class-ref in AppDelegate. o