我遇到了一个奇怪的问题,当我的应用程序第一次加载时,无论你点击多少次,都无法在搜索栏中输入文本,搜索栏嵌套在导航栏中。
我的应用程序也使用一个标签栏,当你切换标签,然后回到与搜索栏的标签,它允许你输入文本。。。你知道是什么引起的吗?
以下是搜索栏的代码:

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(),这就导致了问题!我想是因为子视图的布局不正确。希望这能帮助任何和我有类似问题的人。

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

10-12 04:28