【问题标题】:UISearchController is looking wrong after touch触摸后 UISearchController 看起来不对
【发布时间】:2015-09-25 01:41:21
【问题描述】:

我以编程方式将UISearchController 添加到UIViewController 中,当UIViewController 加载时一切都很好,但是当我在UISearchController 中触摸UISearchBar 时,它移动到底部并且看起来不对。我是第一次在代码中添加 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

【问题讨论】:

    标签: ios xcode swift user-interface swift2


    【解决方案1】:

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

    @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
        })()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2010-10-29
      相关资源
      最近更新 更多