我正在尝试将两个按钮和一个SearchBar添加到我的导航控制器,但对齐方式不正确。我已经尝试了几乎所有的东西,比如改变SearchBar,UIButton的大小或改变Y的定位,但它没有用。知道如何解决这个问题吗? 我使用的是iOS 11和Xcode 9
searchController = UISearchController(searchResultsController: nil)
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 200, height: 30)
searchController?.delegate = self
searchController?.searchResultsUpdater = self
let refineButton = UIButton.init(type: UIButtonType.custom)
refineButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
refineButton.setImage(#imageLiteral(resourceName: "settings-button"), for: UIControlState.normal)
refineButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
refineButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
let refineItem = UIBarButtonItem(customView: refineButton)
navigationItem.leftBarButtonItem = refineItem
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.sizeToFit()
答案 0 :(得分:0)
请检查:
override func viewWillLayoutSubviews() {
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
searchController?.searchBar.sizeToFit()
}