Pascal Bayer
Pascal Bayer

Reputation: 2613

Buttons on top of UIPopoverController

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

Thanks for helping me!

Upvotes: 8

Views: 16875

Answers (6)

Edwin O.
Edwin O.

Reputation: 5276

This worked!

//Determine how to present this view based on device
if ([UIDevice currentDevice ].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    BNRAssetTypeViewController *contentViewController = [[BNRAssetTypeViewController alloc] init];
    UINavigationController *popOverNavigation = [[UINavigationController alloc] initWithRootViewController:contentViewController];

    self.assetPickerPopover = [[UIPopoverController alloc] initWithContentViewController:popOverNavigation];

    [self.assetPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

then in the init function of contentViewController add this

//add a barbutton item which will help in adding new type

UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNew:)];

//set bar item to right side of navbarite
self.navigationItem.rightBarButtonItem =bbi ;

Upvotes: 0

David
David

Reputation: 226

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];

Upvotes: 21

Cole
Cole

Reputation: 726

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];
}

Upvotes: 3

Daniel
Daniel

Reputation: 22395

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...

Upvotes: 3

Jer K.
Jer K.

Reputation: 316

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];

Upvotes: 14

kennytm
kennytm

Reputation: 523184

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.

Upvotes: 5

Related Questions