7

On an iPad application, I want to be able to have multiple buttons in the top bar of a popover. I'm launching it like this:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

pop = [[UIPopoverController alloc] initWithContentViewController:nc];
[pop presentPopoverFromRect:CGRectInset([tableView rectForRowAtIndexPath:indexPath], 10, 10)
                     inView:tableView
   permittedArrowDirections:UIPopoverArrowDirectionAny
                   animated:YES];
pop.delegate = self;
[nc release];

In the viewDidLoad I want to set the titleView to contain multiple UIBarButtonItems. This is ok on a normal UINavigationController but I need to be able to do this and keep the custom style of the navigation bar on the popover.

I have tried setting the rightBarButtonItem to have a toolbar that contains buttons, but they take the format of the toolbar, which itself will not take the format/style of the popover.

atxe
  • 5,029
  • 2
  • 36
  • 50
Lee
  • 2,204
  • 13
  • 15
  • Can you specify what do you mean by "format/style", and how do you set them to popover? – Zapko May 31 '11 at 06:56
  • Ignore my question for a minute, create a popover with a navigation controller in it. You'll see the shine of the buttons matches the shine of the border/arrow automatically. Now, set one of the nav. controller buttons to be a, say, UIToolBar (common hack to put multiple buttons in). The format of the buttons in that toolbar will NOT match the shine of the popover. – Lee Jun 01 '11 at 09:48

4 Answers4

1

It's a bit hackish, but you can create a truly transparent UIToolbar by subclassing and overriding drawRect: with an empty method. This will still draw the UIBarButtonItems on the toolbar, but not the toolbar itself.

Then, as Nils describes, you can create a bar button item out of the custom toolbar instance and add it as the rightBarButtonItem.

Benjamin Mayo
  • 6,649
  • 2
  • 26
  • 25
0

Why you don't use a custom UIView with a UIToolbar or UINavigationBar instead of UIPopoverController?

The effect, is the same!

elp
  • 8,021
  • 7
  • 61
  • 120
  • Because the popover adds it's own gloss, arrow et. all for free. As mentioned it also themes the toolbar/navigation bar with the gloss - so not the same at all. – Lee May 26 '11 at 10:08
  • @Lee Right, but it's an easy alternative! :) Read this: http://stackoverflow.com/q/4769169/88461 – elp May 26 '11 at 10:30
0

First you need to set a delegate to the UINavigationController and then implement the nabigationController:willShowViewController:animated: method. In this method you can adjust the left and right bar button items.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    viewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] 
                                                        initWithTitle:@"Image Source" 
                                                        style:UIBarButtonItemStylePlain 
                                                        target:self 
                                                        action:@selector(cancelPressed)] autorelease]; 
}
Cory Powers
  • 1,140
  • 8
  • 14
  • This isn't answering the question. I specifically want to put customise the left/right bar button item views with multiple buttons and have the view style drill down to them. – Lee May 30 '11 at 10:56
0

I had the same problem, and here is what I did :

UIBarButtonItem *uiBarRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(forcerefresh)];
UIBarButtonItem *uiBarCurl = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:@selector(showlist)];

UIToolbar_transparent* tools = [[UIToolbar_transparent alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
tools.barStyle = UIBarStyleBlackTranslucent;
tools.translucent = YES;
tools.backgroundColor = [UIColor clearColor];
tools.tintColor = self.navigationController.navigationBar.tintColor;
[tools setItems:[NSArray arrayWithObjects:flexItem,uiBarRefresh,uiBarCurl,nil] animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

This way you avoid the dull grey background, the pesky white underline, and you can completely customize how you want them positioned, by using fixed or flex spacings.

Best of luck with your project :]

Nils Munch
  • 8,805
  • 11
  • 51
  • 103
  • Shame you missed the bounty there. My only question is... Is your UIToolbar_transparent the trick to it all. If so, is it available? Doing this with UIToolbar normal results in http://cl.ly/7Gky – Lee Jun 02 '11 at 06:06
  • Yes, but this is true transparent, and looks quite nicely :] – Nils Munch Jun 02 '11 at 09:17
  • My question is - is that class available anywhere ;) – Lee Jun 02 '11 at 11:39
  • My guess is that UIToolbar_transparent is a subclass of UIToolbar which overrides -drawRect: with an empty method as mentioned by @Benjamin in the accepted answer. – cduck Jul 01 '11 at 19:14