触摸后UISearchController看起来不对劲

时间:2015-09-20 11:41:18

标签: ios xcode swift user-interface swift2

我以编程方式UISearchController添加到UIViewController,加载UIViewController时一切都很好,但当我触及UISearchBar中的UISearchController时,它会移至最低点看起来不对劲。我是第一次在代码中添加UI元素。

    override func viewDidLoad() {
    super.viewDidLoad()
    self.definesPresentationContext = true
    searchController = ({
        let controllerSearch = UISearchController(searchResultsController: nil)
        controllerSearch.searchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 44)
        controllerSearch.delegate = self
        controllerSearch.searchBar.delegate = self
        controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Default
        controllerSearch.dimsBackgroundDuringPresentation = false
        controllerSearch.hidesNavigationBarDuringPresentation = false
        controllerSearch.searchResultsUpdater = self
        controllerSearch.searchBar.sizeToFit()
        self.view.addSubview(controllerSearch.searchBar)
        return controllerSearch
    })()
}

虽然我做的是UISearchController的控制台显示框架,它显示正确的结果(0.0,20.0,320.0,44.0)请帮助我任何人。 我仍然尝试过,但结果是一样的。

  let frame = CGRectMake(0, 20, self.view.frame.size.width, 44)
  controllerSearch.searchBar.frame = frame

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。我添加了以下元素

@IBOutlet weak var navigationItemSearch: UINavigationItem!

并更改了我的UISearchController

   searchController = ({
        let controllerSearch = UISearchController(searchResultsController: nil)
        controllerSearch.delegate = self
        controllerSearch.searchBar.delegate = self
        controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Prominent
        controllerSearch.dimsBackgroundDuringPresentation = false
        controllerSearch.hidesNavigationBarDuringPresentation = false // false
        controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Default
        controllerSearch.searchResultsUpdater = self
        // placing
        controllerSearch.searchBar.sizeToFit()
        navigationItemSearch.titleView = controllerSearch.searchBar
        //
        controllerSearch.searchBar.spellCheckingType = UITextSpellCheckingType.Yes
        controllerSearch.searchBar.autocapitalizationType = UITextAutocapitalizationType.None
        controllerSearch.searchBar.keyboardType = UIKeyboardType.WebSearch
        return controllerSearch
    })()