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

后端 未结 3 670
清歌不尽
清歌不尽 2021-01-02 12:46

I\'ve added a search bar to my navigation.titleView

    self.navigationItem.titleView = searchBar

There\'s also a BackBarButtonItem with ti

相关标签:
3条回答
  • 2021-01-02 13:28
    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  
      }  
    }
    
    0 讨论(0)
  • 2021-01-02 13:41
    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
    }
    

    0 讨论(0)
  • 2021-01-02 13:44

    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

    0 讨论(0)
提交回复
热议问题