views:

1239

answers:

5

Hi, I want to add two Buttons on top of UIPopoverController like it is shown in following screenshots: HTML Edit

Thanks for helping me!

+1  A: 

Use a UINavigationController as the pop-over. Then, access the .navigationBar property of the navigation controller, get the .topItem, and set its .leftBarButtonItem and .rightBarButtonItem.

KennyTM
I know this way already and that's not what I want. The Buttons on the screenshot are implemented directly in the header of the uipopovercontroller. There should be an other solution!
pbcoder
@pbcober: ?? The only special thing is the button style is UIBarButtonItemStyleDone.
KennyTM
@KennyTM: Really??? If I use an Navigation Controller there is an shadow between the navigationBar and the border of the popover. and the two edges under the navigationBar aren't rounded like the two on the screenshot. Here's a screenshot of my own app: http://ScrnSht.com/fbvrpi
pbcoder
@pbcoder: That's odd. What is the `.barStyle` of the navigation bar?
KennyTM
i woudlnt recommend using UINavigationCOntroller as a popover, they dont behave well in the popover, i tried to do this and the popover keeps resizing when i try to push new VC on the stack, and does not listen to the popovers contentSize property...use a UIToolBar instead....
Daniel
+3  A: 

You need to initialize your popover with a UINavigationController directly. Then set the root view to your custom view controller.

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourViewController];     
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
Jer K.
Okay, worked fine! But how I can add the Buttons/title to the popover now? navigationController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]...]...] doesn't work.
pbcoder
see my comment above about using navigationcontrollers in popovers
Daniel
@pbcoder if you still choose this route, use: navigationController.navigationBar.topItem.leftBarButtonItem = ... and set the title on your root vc via the "title" property.
Jer K.
+1  A: 

I wouldnt use a navigationcontroller like the previous posters suggested, apple recommends not using navigationcontrollers on ipad (with good reason) it doesnt behave as youd expect when pushing VC into the stack when used in popovers, now you dont really want to use the "navigation" aspect of it, but i wouldnt use navigationcontroller just because uw ant the bar....Use a UIToolBar instead, and set its buttons to whatever you want...no need to use a navigation controller here...

Daniel
But I think an UIToolBar wouldn't integrate as good as an UINavigationController into the Popover. If I use an Toolbar, there is the bordershadow I was talking about in my previous comments and there should be a solution to add buttons to navigationControllers too
pbcoder
I see what you mean...that seems to be a color issue, you sohuld be able to change the color of the background of a UIToolbar or maybe set the barStyle to blackTransparent ...u can add buttons to navcontrollers , but i would probably use a toolbar
Daniel
No Toolbar would't work. I've found the solution. You have to put the code into the viewdidload of the class from the view you show inside the popover
pbcoder
right...u can do that with a toolbar lol..
Daniel
Why would Apple recommend not using navigationcontrollers on iPad ? I see no such thing in iPad User Experience Guidelines. What I see is that we should consider to flatten the hierarchy as mush as possible, but when you look at the settings app, they use navigation controllers all over the place. I rather think navigation controllers are most appropriate in pop over controller, that's the point of popovercontroller ! Along with a UISplitViewController, it allows you to drill down through the hierarchy...
Unfalkster
+1  A: 

When I do this my navBar doesn't seem to fit properly inside of the UIPopoverController, as shown in the below:

http://www.flickr.com/photos/coleorton/4752223066/

Here's what I'm doing:

// alloc the Direct Reports view controller.
ToolsViewController *toolsViewController = [[[ToolsViewController alloc] init] autorelease];

UINavigationController *toolsNavController = [[[UINavigationController alloc] initWithRootViewController:toolsViewController] autorelease];
toolsNavController.title = @"Tools";
toolsNavController.view.frame = CGRectMake(0.0, -10.0, 320.0, POPOVER_HEIGHT);

if(![self.toolsPopoverController isPopoverVisible]){
    // show popover

    self.toolsPopoverController = [[[UIPopoverController alloc] initWithContentViewController:toolsNavController] autorelease];
    self.toolsPopoverController.delegate = self;
    self.toolsPopoverController.popoverContentSize = CGSizeMake(320.0, POPOVER_HEIGHT);
    [self.toolsPopoverController presentPopoverFromBarButtonItem:sender 
                                     permittedArrowDirections:UIPopoverArrowDirectionAny 
                                                     animated:YES];

} else {
    // close popover
    [self.toolsPopoverController dismissPopoverAnimated:YES];
}
Cole
Do you set the color of your navController?
pbcoder
i set it's tint property. does that matter? toolsNavController.navigationBar.tintColor = [UIColor colorWithRed:6/255.0 green:122/255.0 blue:180/255.0 alpha:1];
Cole
that did the trick... commenting that line out made the popover look great.
Cole
It's not that the bar doesn't fit, it's just that popOverControllers seem to be optimized to fully integrate the navigation bar, meaning that the popOverController borders are part of the navigation bar. Indeed, when the navigation bar is not the same color as the popOverController, there is a weird layout effect (maybe someone should file a bug about that ?). As pbcoder hinted, just make sure the navigation bar tintColor is nil.
Unfalkster
+1  A: 

Add your view controller to a UINavigationController, then add the Navigation Controller to the UIPopoverController. Then in your UIViewController's viewDidLoad method, put this code in:

UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleBordered target:self action:@selector(okayButtonPressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelButtonPressed)];

self.navigationItem.title = @"My Title";

[self.navigationItem setLeftBarButtonItem:cancelButton animated:NO];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];

[cancelButton release];
[okButton release];
David