【问题标题】:Set textfield in UISearchBar在 UISearchBar 中设置文本字段
【发布时间】:2015-09-11 05:38:11
【问题描述】:

我正在尝试将 SearchController 添加到 UINavigationBar。我试图在 UINavigationController 的后退按钮之后设置 UISearchBar 的 UITextField。我想要后退按钮后的更多空间

当我开始搜索时,它显示为

而我应该能够查看后退按钮。仅应减小文本字段宽度。同样在 Cancel 之后,它应该再次回到初始布局。而显示如下:

下面是我的代码

var searchResultController = UISearchController()
    
self.searchResultController = ({
        
    let controller = UISearchController(searchResultsController: nil)
    controller.searchResultsUpdater = self
    controller.dimsBackgroundDuringPresentation = false
    controller.searchBar.sizeToFit()
    controller.searchBar.delegate = self
        
    controller.hidesNavigationBarDuringPresentation = false
    self.navigationController?.navigationBar.addSubview(controller.searchBar)
        
    return controller
})()

override func viewDidAppear(animated: Bool) {
    for subView in searchResultController.searchBar.subviews{
        for subsubView in subView.subviews  {
            if let textField = subsubView as? UITextField {
                var bounds: CGRect
                bounds = textField.frame
                bounds.size.width = self.view.bounds.width - 50    
            }
        }

    }
}

请告诉我如何解决这个问题。

提前致谢

【问题讨论】:

    标签: swift uitextfield uisearchbar uisearchcontroller


    【解决方案1】:

    为了设置 UIsearchBar 把它添加到 Navigation 的 titleView 中

    self.navigationItem.titleView = controller.searchBar
    

    为了移除取消按钮,我们可以使用 UISearchControllerDelegate 方法

    func didPresentSearchController(searchController: UISearchController) {
        searchController.searchBar.showsCancelButton = false
    }
    

    希望这可以帮助任何人。

    【讨论】:

      猜你喜欢
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2016-06-13
      • 2023-03-31
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多