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

时间:2017-07-01 11:54:39

标签: ios swift uitextfield uinavigationbar uisearchbar

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

我的应用程序也使用标签栏,当您切换标签时,然后返回带有搜索栏的标签,它允许您输入文字...任何想法导致了什么?

以下是searchBar的代码:

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

}

1 个答案:

答案 0 :(得分:2)

好了很多,我发现在我的自定义UITabBarController中,我使用了override func viewWillAppear(_ animated: Bool)而没有添加super.viewWillAppear(),这导致了问题!我假设因为子视图没有正确布局。希望能帮助那些遇到类似问题的人。