-->

UINavigationBar with buttons as title.

2020-08-17 17:56发布

问题:

I have done some digging and can't seem to get anything useful.

What I am trying to do is pretty basic but I'm not sure if their is:

1. An easier way

2. Or if Apple will give it a "no no"

I have a view that is controlled via UINavigationController (obviously from the title of this post) and in it has an object/view.

The view is fairly congested already with content. Not really congested but no more room to fit things comfortably.

The user needs to be able to rate this object by using a different number of starts (1-5).

The ideal way I would see this happening would be to have 5 star buttons (with images of course) set in the UINavigationBar title position (centered).

So look something like this:

___________________________________
|                                 |
| Back      * * * * *     Action  |
|_________________________________|
|                                 |
|         view down here          |
|                                 |

Any input would be very much appreciated and as always, thank you for your time!

回答1:

You can just create your custom widget with the 5 star rating functionality and add it to the navigationItem

self.navigationItem.titleView = customWidget;


回答2:

On top of my head, my suggestion would be that you try using UIToolbar and set it on top, and make it look like a navigationBar. It's fairly easy to implement it and add UIButtons and actions to it.



回答3:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(chooseDataCenter:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"点我选择数据中心>" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 40.0);

self.navigationItem.titleView =button;