To create a popover view
Go to your storyboard, set a button in view controller(on click of this button we will pop a popover view).
![](http://findnerd.s3.amazonaws.com/imagedata/4961/4961.png)
Make an IBAction form this button to your view controller
eg:- (IBAction)popoverButtonClicked:(id)sender{
}
Now connect this action to your ViewController like this :-
![](http://findnerd.s3.amazonaws.com/imagedata/4971/4971.png)
Now add new file (source -> Cocoa Touch Class -> click “Next” -> make subclass of “UIViewController”)name it “popoverViewController”
Drag a new view controller from object library and give this class name “popoverViewController” and Storyboard ID “popoverViewController”
![](http://findnerd.s3.amazonaws.com/imagedata/4966/4966.png)
Now go to its attribute inspector and change size to "Freeform" and status bar to "none", now checkmark “Use Preferred Explicit Size” and set its width to 120 and height 90(you can change it according to your requirement). Place a label to it.
![](http://findnerd.s3.amazonaws.com/imagedata/4968/4968.png)
You can place anything in popover view which you want to display(in this case put a UILabel).
![](http://findnerd.s3.amazonaws.com/imagedata/4969/4969.png)
Now go to your view controller and make a property of “UIPopoverPresentationController”
eg: @property (nonatomic, strong) UIPopoverPresentationController *popover;
And edit your “popoverButtonClicked:” function
- (IBAction)popoverButtonClicked:(id)sender {
popoverViewController *popVC = [self.storyboard instantiateViewControllerWithIdentifier:@"popoverViewController"];
UINavigationController *popNav = [[UINavigationController alloc]initWithRootViewController: popVC];
popVC.preferredContentSize = CGSizeMake(120,90);
popNav.modalPresentationStyle = UIModalPresentationPopover;
_popover = popNav.popoverPresentationController;
_popover.delegate = self;
_popover.sourceView = self.view;
CGRect frame = [sender frame];
frame.origin.y = frame.origin.y-4;//set the position of PopoverView(you can change it according to your requirement.)
_popover.sourceRect = frame;
popNav.navigationBarHidden = YES;
[self presentViewController: popNav animated:YES completion:nil];
}
Now run your app and click popover button, you will see output like this.
![](http://findnerd.s3.amazonaws.com/imagedata/4970/4970.png)
1 Comment(s)