UIPopoverController(Basics)
This is sample of UIPopoverController
UIPopoverController is popup-like dialog.
This is valid on iPad UI. If you use iPhone UI, exception will happen.
Sample
This is just code UI.
PopupViewSampleViewController.h
@interface PopupViewSampleViewController : UIViewController<UIPopoverControllerDelegate> @end
Please implements UIPopoverControllerDelegate.
PopupViewSampleViewController.m
@interface PopupViewSampleViewController ()
@property (nonatomic) UIPopoverController *popup;
@end
@implementation PopupViewSampleViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
[button setBackgroundColor:[UIColor blueColor]];
[button setTitle:@"OpenPop" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openPop:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UI Event
-(void)openPop:(id)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIViewController *controller = [[UIViewController alloc] init];
[controller.view setBackgroundColor:[UIColor grayColor]];
controller.view.frame = CGRectMake(100, 100, 150, 50);
self.popup = [[UIPopoverController alloc] initWithContentViewController:controller];
self.popup.popoverContentSize = CGSizeMake(150, 100);
self.popup.delegate = self;
[self.popup presentPopoverFromRect:CGRectMake(100, 100, 150, 50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// UIPopoverArrowDirectionAny
}
}
#pragma mark - Popover
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
self.popup = nil;
}
@end
Arrow Direction
This popup has arrow. We can select some types of arrow.
| Type |
|---|
| UIPopoverArrowDirectionAny |
| UIPopoverArrowDirectionDown |
| UIPopoverArrowDirectionLeft |
| UIPopoverArrowDirectionUnknown |
| UIPopoverArrowDirectionUp |

