How do I add a left bar button without overriding the natural back button?

◇◆丶佛笑我妖孽 提交于 2019-12-19 19:45:45

问题


I have a project that uses UIViewControllers embedded in navigation view controllers, so the back button is automatically set up for me whenever I segue into any detail of my tableviews.

Now I want to add an Edit button beside the back button. I've already put an Unassign button on the right side, and since the word "Unassign" is quite long, I'd rather fit the Edit button beside the back button on the left side.

I know I can programmatically add bar buttons with navigationItem.leftBarButtonItem = editButton but that overrides the back button.

I've also tried appending the editButton to the navigationItem.leftBarButtonItems array, but that doesn't work too (since the leftBarButtonItems array isn't set in the first place).

My next approach was to find the back button and manually add it to the leftBarButtonItems array, so I tried using navigationItem.leftBarButtonItems = [self.navigationItem.backBarButtonItem!, editButton], but that only adds a space between the editButton and the left side. No back button.

How do I add in a left bar button while leaving the back button untouched? Is there a more elegant way than manually coding a new back button with self.navigationController?.popViewControllerAnimated(true) and adding it to the leftBarButtonItems array?


回答1:


By default, the leftItemsSupplementBackButton property is false. In this case, the back button is not drawn and the left item or items replace it. If you would like the left items to appear in addition to the back button (as opposed to instead of it) set leftItemsSupplementBackButton to true.

@property(nonatomic) BOOL leftItemsSupplementBackButton

you can use something like this

navigationItem.leftBarButtonItem = editButton
//or
navigationItem.leftBarButtonItems = [editButton]

self.navigationItem.leftItemsSupplementBackButton = true



回答2:


These two lines with do the trick. Set leftItemSupplementBackButton to true, and then add leftBarButtonsItems.

self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItems = [barButton]


来源:https://stackoverflow.com/questions/39072202/how-do-i-add-a-left-bar-button-without-overriding-the-natural-back-button

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