How to create a left aligned title with prefersLargeTitles in navigationBar?

巧了我就是萌 提交于 2021-02-08 10:40:34

问题


I'm using prefersLargeTitles on my navigation bar, When it collapse, I want to align the title to the left of the navigation bar,

What I did is I create a leftBarButtonItem with a custom view of label and set the title color to .clear, the problem is, that label is visible even if the large titles didn't collapsed yet.

This is my code for adding navigation item as my title

let label = UILabel()
label.textColor = UIColor.black
label.text = "my left title"
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: label)

or the other question for this post is, how can I hide the leftBarButtonItem if the large title didn't collapsed yet?


回答1:


Here's a link which can help you to find a way out as you want : Change title according to navigation bar.

Some basic things would like to add is you can set navigation bar title to empty string or something else as you want and make your custom view visible if the bar is collapsed other then that make it invisible. You can check in the link how to observe if navigation bar is collapsed or not.

One more important thing is you should remove observers if you are switching to another screen or it can cause memory leaks.



来源:https://stackoverflow.com/questions/63717269/how-to-create-a-left-aligned-title-with-preferslargetitles-in-navigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!