Custom title as a large title in the new iOS 11 navigation bar

I use the button as a title for my UITableViewController , which opens a drop-down list of categories. Selecting a category filters the contents in a table for the selected category.

The button shows the name of the selected category plus a small arrow similar to how iBooks looked (or maybe it still looks? I didn’t use it at that time). Therefore, I would like it to have the same behavior as the standard header, and to have it large first and reset when scrolling through the table view.

Is there any way to do this?

thanks

+5
source share
4 answers

Touch seems to be broken for navigationItem.titleView. Gestures, events, events and buttons - nothing works

+3
source

Seems like a bug in iOS 11: https://forums.developer.apple.com/thread/82466

I temporarily executed this workaround:

  private lazy var navBarActionButtonIOS11: UIButton = { button.addTarget(self.navTitleView, action: #selector(self.navTitleView.didTapView), for: .touchUpInside) return button }() [...] navigationItem.titleView = navTitleView if #available(iOS 11.0, *), let navBar = navigationController?.navigationBar { navBarActionButtonIOS11.removeFromSuperview() navBar.addSubview(navBarActionButtonIOS11) navBarActionButtonIOS11.center.x = navBar.center.x } 

Another solution would be to simply assign the UIButton to the navigationItem.titleView function directly.

I hope Apple fix it soon!

+2
source

It seems that due to the new large headers, IOS11 requires restrictions on the user view in the navigationItem.titleView file.

Do this for example:

 customView.widthAnchor.constraint(equalToConstant: 200).isActive = true customView.heightAnchor.constraint(equalToConstant: 44).isActive = true self.navigationItem.titleView = customView 

Please note that this should be done both in width and in height.

It should work. No need to add a button, at least in my case ...

This was suggested by Apple to make sure you don't have custom zero-size views. See Slide 33 at https://developer.apple.com/videos/play/wwdc2017/204/

+2
source

Well, I had the same problem. I have UIButtons in UINavigationItem.titleView and they don’t respond to touches at all. The problem is that the view where these buttons are sized (0,0) due to the automatic layout. Therefore, to fix this problem, you need to add an additional view to your custom view, name it β€œcontentView” and put all your controls inside this contentView. In addition, the contentView must have a certain size with restrictions. A quick test is to add width and height constraints to the contentView. And everything works again.

Hope this helps someone.

+1
source

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


All Articles