How to resize titleView in navigation bar. Because the gap between titleView and backButton in navigationBar

I added a search bar to my navigation.titleView file

self.navigationItem.titleView = searchBar 

There is also a BackBarButtonItem element with the name = ""

  self.navigationItem.backBarButtonItem?.title = "" 

But then there is a space between the Back Button and SearchBar , for example: Between button

I think a space appears here because there is room for a title of backBarButtonItem (because my title is null ", but the space is still there)

So, I want to ask how to skip this gap? I want to make my SearchBar closer to my backBarIcon

Thank you very much!

EDIT 1: I'm trying to change the search frame, but it doesn't work

This is my code.

  //Change searchBar 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) 
+7
source share
3 answers
 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 } 

enter image description here

+9
source

You cannot do this, there is a default space that we cannot change if we have a return 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 a screenshot enter image description here

0
source
 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
source

Source: https://habr.com/ru/post/1264663/


All Articles