设置UISearchBar高度会破坏RightBarButton项的位置

时间:2019-03-09 12:22:55

标签: ios swift uinavigationcontroller

我有一个UISearchBar作为UINavigationController的标题视图。当我将UIsearchBar的高度设置为44时。UIBarButtonItemUISearchBar之间的缝隙变成废墟。

什么原因导致此问题?

我如何设置工具栏:

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

错误: Ruined one

右一 Second one

0 个答案:

没有答案