如何更改导航栏中titleView的大小。因为有在的导航栏
我添加了一个搜索栏我navigation.titleView titleview的和后退按钮之间的空隙如何更改导航栏中titleView的大小。因为有在的导航栏
self.navigationItem.titleView = searchBar
还有与标题BackBarButtonItem =“”
self.navigationItem.backBarButtonItem?.title = ""
但后来有“再差距Back Button
和SearchBar
之间,就像这样:
我想是因为那里的0123空间,在这里出现的差距的backBarButtonItem
(因为我的title
为空“”但空间仍然存在)
所以我想问一下如何忽略那个差距?我想让我的searchBar
更接近我的
非常感谢!
编辑1: 我试图改变搜索栏的框架,但它不工作
这是我的代码
//Change searchBar's frame
let titleViewFrame = (searchController.searchBar.frame)
searchController.searchBar.frame = CGRect(x: titleViewFrame.minX - 20.0, y: titleViewFrame.minY, width: titleViewFrame.width + 20.0, height: titleViewFrame.height)
override func viewDidLoad() {
super.viewDidLoad()
let container = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 22))
let searchBar = UISearchBar()
searchBar.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(searchBar)
let leftButtonWidth: CGFloat = 35 // left padding
let rightButtonWidth: CGFloat = 75 // right padding
let width = view.frame.width - leftButtonWidth - rightButtonWidth
let offset = (rightButtonWidth - leftButtonWidth)/2
NSLayoutConstraint.activate([
searchBar.topAnchor.constraint(equalTo: container.topAnchor),
searchBar.bottomAnchor.constraint(equalTo: container.bottomAnchor),
searchBar.centerXAnchor.constraint(equalTo: container.centerXAnchor, constant: -offset),
searchBar.widthAnchor.constraint(equalToConstant: width)
])
self.navigationItem.titleView = container
}
为什么我不能点击搜索文本,好像有一个容器会阻止它 –
它运行时看起来不错,但它不响应输入。另外,注意到如果你触摸左侧,它就像后退按钮被选中一样。它就像它不在那里。 –
@JeffMuir由于默认后退按钮框架大小比它看起来大,您可以使用您自己的左侧栏按钮而不是系统,以避免此问题 –
你不能做到这一点,也被赋予了默认空间,我们无法改变如果我们有后退按钮。
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back")
self.navigationController?.navigationBar.tintColor = UIColor.lightGray
尝试使你的搜索栏宽通过调整其'frame'。 –
我做了IT,但它仍然无法正常工作 –
请看下面的@Peter Chang的解决方案,这是通过AutoLayout完成的。 –