ios - 导航栏中的 UISearchBar 无法输入文本

标签 ios swift uitextfield uinavigationbar uisearchbar

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

我的应用程序也使用标签栏,当您切换标签然后返回带有搜索栏的标签时,它允许您输入文本...知道是什么原因造成的吗?

这是搜索栏的代码:

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

}

最佳答案

好吧,经过一番折腾,我发现在我的自定义 UITabBarController 中,我使用了 override func viewWillAppear(_ animated: Bool) 而没有添加 super.viewWillAppear()这导致了问题!我认为是因为 subview 的布局不正确。希望能帮助任何与我有类似问题的人。

关于ios - 导航栏中的 UISearchBar 无法输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44860623/

相关文章:

swift - 如何检测 NSMenu 是否关闭?

ios - stringFromDate 在特殊设备上崩溃

iphone - 当 UITextField 失去焦点时隐藏键盘

ios - 为 OpenGL ES/Cocos2d 优化纹理的方法

ios - 重复淡入淡出效果不起作用

ios - 使用 TesseractOCR 的链接器命令失败,退出代码为 1(使用 -v 查看调用)

ios - 我需要在 diawi 中的什么地方放置 UDID 信息?

ios - 如何将 'Any?' 转换为具有灵活通用属性的结构?

iphone - 如何在 UITableViewCell 中嵌套 UITextField?

ios - UiTextfield-首字母必须为0