简体   繁体   English

如何在导航栏中更改titleView的大小。 因为navigationBar中的titleView和backButton之间存在差距

[英]How to change the size of titleView in navigation bar. Because there's a gap between titleView and backButton in navigationBar

I've added a search bar to my navigation.titleView 我在navigation.titleView中添加了一个搜索栏

    self.navigationItem.titleView = searchBar

There's also a BackBarButtonItem with title = "" 还有一个标题=“”的BackBarButtonItem

    self.navigationItem.backBarButtonItem?.title = ""

But then there're gap between Back Button and SearchBar , like this: 但是Back ButtonSearchBar之间存在差距,如下所示: Back Button和SearchBar之间存在差距

I Think that the gap appears here because there's space for title of backBarButtonItem (because my title is null "" but the space still there) 我认为差距出现在这里是因为backBarButtonItem title空间(因为我的title为空“”但空间仍然存在)

So I want to ask how to omit that gap? 所以我想问一下如何省略这个差距? I want to make my searchBar nearer my backBarIcon 我想让我的searchBar更接近我的backBarIcon

Thank you so much! 非常感谢!

EDIT 1: I try to change searchBar's frame but it's not working 编辑1:我尝试更改searchBar的框架,但它不起作用

This is my code 这是我的代码

    //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
}

在此输入图像描述

You can't do that, there is a default space given which we cannot change if we have back button. 你不能这样做,有一个默认的空间给我们,如果我们有后退按钮我们不能改变。

 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

Below is the screenshot 以下是截图 在此输入图像描述

class SearchBarContainerView: UIView {  
  let searchBar: UISearchBar  
  init(customSearchBar: UISearchBar) {  
    searchBar = customSearchBar  
    super.init(frame: CGRect.zero)  

    addSubview(searchBar)  
  }

  override convenience init(frame: CGRect) {  
    self.init(customSearchBar: UISearchBar())  
    self.frame = frame  
  }  

  required init?(coder aDecoder: NSCoder) {  
    fatalError("init(coder:) has not been implemented")  
  }  

  override func layoutSubviews() {  
    super.layoutSubviews()  
    searchBar.frame = bounds  
  }  
}  

class MyViewController: UIViewController {  
  func setupNavigationBar() {  
    let searchBar = UISearchBar()  
    let searchBarContainer = SearchBarContainerView(customSearchBar: searchBar)  
    searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)  
    navigationItem.titleView = searchBarContainer  
  }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM