Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a UIPopoverView programmatically in iOS 9

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.58k
    Comment on it

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

     

    Make an IBAction form this button to your view controller

    eg:- (IBAction)popoverButtonClicked:(id)sender{
    }

     

    Now connect this action to your ViewController like this :-

     

     

    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”

     

    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.

                         

     

    You can place anything in popover view which you want to display(in this case put a UILabel).

    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.

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: