我有一个UISearchBar
作为UINavigationController
的标题视图。当我将UIsearchBar
的高度设置为44时。UIBarButtonItem
和UISearchBar
之间的缝隙变成废墟。
什么原因导致此问题?
我如何设置工具栏:
private func setupSearchBar() {
navigationItem.titleView = searchBar
let button = UIButton(type: UIButtonType.custom)
let buttonImage = UIImage(named: "shuffle-search-close")
button.setImage(buttonImage, for: UIControlState.normal)
button.addTarget(self, action: #selector(barCloseButtonTapped(_:)), for: UIControlEvents.touchUpInside)
button.transform = CGAffineTransform.init(rotationAngle: .pi/4)
let rightBarButtonItem = UIBarButtonItem(customView: button)
rightBarButtonItem.customView!.widthAnchor.constraint(equalToConstant: 24).isActive = true
rightBarButtonItem.customView!.heightAnchor.constraint(equalToConstant: 24).isActive = true
self.navigationItem.rightBarButtonItem = rightBarButtonItem
searchBar.showsScopeBar = false // you can show/hide this dependant on your layout
searchBar.placeholder = "Search"
searchBar.returnKeyType = .search
searchBar.delegate = self
searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
self.navigationController?.view.setNeedsLayout()
self.navigationController?.view.layoutIfNeeded()
}