I figured out a way to make popover work on the iPhone and iPad programmatically!
Create a category to make popover available on iPhone (more info here )
//UIPopover+Iphone.h @interface UIPopoverController (overrides) + (BOOL)_popoversDisabled; @end //UIPopover+Iphone.m @implementation UIPopoverController (overrides) + (BOOL)_popoversDisabled { return NO; } @end
Create a button that displays a popover and implements the method that it calls.
ExampleUIViewController.h
@interface ExampleViewController : UIViewController <UIPopoverControllerDelegate> @property (strong, nonatomic) UIButton *detailButton; @property (nonatomic, retain) IBOutlet UIPopoverController *poc;
The UIPopoverController poc should be stored in an instance variable, more details here .
ExampleUIViewController.m
- (void)viewDidLoad { _detailButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_detailButton addTarget:self action:@selector(showPop:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_detailButton]; } -(void)showPop:(UIButton *)button { UIViewController *detailsViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil]; self.poc = [[UIPopoverController alloc] initWithContentViewController:detailsViewController]; [self.poc setDelegate:self]; [self.poc presentPopoverFromRect:_detailButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]; }
- Create a UIViewController that will contain what is displayed inside the popover (in the example, the DetailsViewController element)
Just create it in your project by right-clicking → New File → Objective c class → UIViewController and check the “With XIB” box.
Then, when you click on the button, it will appear next to the button.
Tested OK on iOs5 and above.
Sèb
source share