简体   繁体   中英

Creating popover view controllers for Iphone in Interface Builder

How do I go about doing this? I know I have to give the popover a view controller that it constructs its view from, but I don't know how to construct this view in Interface Builder (like adding the labels and buttons and whatnot).

Do I just do it in the normal storyboard and have a storyboard that's randomly not connected to anything else, sitting off in the corner?

Do I create another storyboard?

Do I create a xib even though my project is all storyboard?

Do I create it programatically?

Since You did not specified target device in your question, I gave you Ipad answer

IPAD:

Go to your storyboard drag and drop a viewcontroller or tableviewcontroller its up to you. Then create a segue from desired viewcontroller on your storyboard to newly dragged/dropped viewcontroller . Choose your segue as popOver .

在此输入图像描述

在此输入图像描述

Make sure you choose an anchor in segue settings like above picture. Then you need to edit size of your popover . If its a uiviewcontroller choose its view,if its a tableviewcontroller choose its tableview on left side of interface builder and edit its size.

First:

在此输入图像描述

Second:

在此输入图像描述

After that customize your popover view controller (dragged/dropped view controller) add button,label whatever you want.

If you are going to do additional things in your popover: Dont forget to create new file -> subclass of uiviewcontroller or uitableviewcontroller. Then associate it with your newly created viewcontroller on storyboard.

在此输入图像描述

IPHONE:

There are no Popover controllers in iphone

But you try to use a third party https://github.com/50pixels/FPPopover solution that mimics popover behaviors in iphone by using QuartzCore

I would try following:

First:

Again drag and drop a uiviewcontoller to your storyboard, then create a new file-> subclass of uiviewcontroller .

This new uiviewcontroller will sit in the corner in your storyboard

Then associate your uiviewcontroller in your storyboard with new Created file , lets say you created a new file name with YourViewController . Give your view a storyboard id and choose its class name.

在此输入图像描述

-(IBAction)buttonClicked:(UIButton*)okButton
{
    //the view controller you want to present as popover
    YourViewController *controller = [[YourViewController alloc] init]; 

    //if [[YourViewController alloc] init]; doesn't work try this
   // UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle:nil];
    //YourViewController *controller = [sb instantiateViewControllerWithIdentifier:@"YourViewController"]; 

    //our popover
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller]; 

    //the popover will be presented from the okButton view 
    [popover presentPopoverFromView:okButton]; 

    //no release (ARC enable)
    //[controller release];
}

Note: I havent used that FPPopover before so dont know how to arrange screen size but there must be further explanation in their documents just read it.

I think the better approach for the iPhone device is, that you create a View Controller whose View has the background colour set to clear. Then in this view you just add a smaller sized view which will give the appearance of a popup.

Presenting this view controller will give the appearance of a popup. This is the approach I took. If the user taps on an area on the parent view that is outside the bounds of the smaller view, you can dismiss the view controller.

iPhone solution

Since iOS 8.0 is is possible to realize this for iPhone apps too!

The target view controller (the one, which should appear in a popover) needs to conform to the ' UIPopoverPresentationControllerDelegate ' protocol and has to implement the message ' adaptivePresentationStyleForPresentationController:traitCollection: '. In addition to this its ' modalPresentationStyle ' should be ' UIModalPresentationPopover ' and the controller should be his own ' UIPopoverPresentationController ' delegate.

After preparing the view controller this way, just create a storyboard segue (type: 'Present As Popover') from your UINavigationBarItem (or anything else) to the target view controller.

在此输入图像描述

Implementation:

@implementation TestViewController

/*
 initWithCoder:

 */
- (instancetype)initWithCoder:(NSCoder *)pDecoder {
    //FLog;

    if (self = [super initWithCoder:pDecoder]) {

        self.modalPresentationStyle = UIModalPresentationPopover;
        self.popoverPresentationController.delegate = self;
    }
    return self;
}


#pragma mark - POPOVER PRESENTATION CONTROLLER DELEGATE

/*
 adaptivePresentationStyleForPresentationController:traitCollection:

 */
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)pController
                                                               traitCollection:(UITraitCollection *)pTraitCollection {
    //FLog;

    return UIModalPresentationNone;
}

// ...

@end

Of course the delegate protocol can be implemented in some other class also, but this way is the easiest one :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM