【问题标题】:UISearchBar in Navigation Bar cannot enter text导航栏中的 UISearchBar 无法输入文本
【发布时间】:2017-07-01 11:54:38
【问题描述】:

我遇到了一个奇怪的问题,当我的应用首次加载时,无论您点击多少次都无法在搜索栏中输入文本,搜索栏嵌套在导航栏中。

我的应用也使用标签栏,当您切换标签然后返回到带有搜索栏的标签时,它允许您输入文本...任何想法是什么原因造成的?

搜索栏的代码如下:

func setupSearchBar(){

    let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTableViewController
    resultSearchController = UISearchController(searchResultsController: locationSearchTable)
    resultSearchController?.searchResultsUpdater = locationSearchTable

    searchBar = resultSearchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Location"
    searchBar.isTranslucent = true
    searchBar.isUserInteractionEnabled = true

    for subView in searchBar.subviews{

        for subsubView in subView.subviews{

            if let textField = subsubView as? UITextField{

                var currentTextFieldBounds = textField.bounds
                currentTextFieldBounds.size.height = 40.0
                textField.bounds = currentTextFieldBounds
                textField.borderStyle = UITextBorderStyle.none
                textField.textAlignment = NSTextAlignment.left
                textField.font = UIFont(name: "System", size: 25.0)
                textField.textColor = theme?.textColour

            }
        }
    }

    self.navigationController?.navigationBar.setBarColour(colour: (theme?.tabBarColour)!, tint: (theme?.textColour)!)

    navigationItem.titleView = resultSearchController?.searchBar
    navigationItem.titleView?.bringSubview(toFront: (resultSearchController?.searchBar)!)
    searchBar.delegate = self
    searchBar.showsSearchResultsButton = true
    searchBar.setImage(#imageLiteral(resourceName: "location_icon.png"), for: UISearchBarIcon.resultsList, state: UIControlState.normal)

    resultSearchController?.hidesNavigationBarDuringPresentation = false
    resultSearchController?.dimsBackgroundDuringPresentation = true
    definesPresentationContext = true

    locationSearchTable.mapView = mapView
    locationSearchTable.handleMapSearchDelegate = self

}

【问题讨论】:

  • “无法输入文本”是什么意思,键盘没有显示或者您无法在文本框中输入任何内容?
  • 键盘根本不显示。只有在您更改了选项卡之后。稍后我会发布一些代码,我现在不在家。
  • 请添加屏幕截图和更多信息...不难发现问题
  • 屏幕截图无济于事,您点击搜索栏输入文字,没有任何反应。我已经尝试过 isUserInteractionEnabled、zPositions 和 layoutsubviews 等,但没有任何效果。
  • 你在真 iPhone 上试过了吗?因为模拟器有时会在它应该显示的时候不显示键盘

标签: ios swift uitextfield uinavigationbar uisearchbar


【解决方案1】:

好吧,经过一番折腾,我发现在我的自定义 UITabBarController 中我使用了override func viewWillAppear(_ animated: Bool) 而没有添加super.viewWillAppear(),这导致了问题!我假设因为子视图没有正确布局。希望对遇到和我有类似问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多