Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create popover in iPhone app?

I'm looking for popover in iPhone and i want to make it like iOS 5 Reader feature:

enter image description here

After little research i found WEPopover and FPPopover but i'm looking if there anything like this API built-in iphone SDK.

like image 936
Xtrician Avatar asked Jul 18 '12 18:07

Xtrician


People also ask

How do I show popover in iOS?

Add a new file to the project, Select File -> New ->File and then select iOS -> Source -> Cocoa Touch Class. Name it PopoverViewController and make it a subclass of UIViewController. Go back to Main. storyboard and select the added View Controller.

What is a popover iOS?

A popover is a transient view that appears above other content onscreen when people click or tap a control or interactive area.

What is a UI popover?

A popover is a transient view that shows on a content screen when a user clicks on a control button or within a defined area. In the OS design system, a popover is preferred in big screens (tablet size or bigger).

What is iOS modal?

Modality is a design technique that presents content in a separate, focused mode that prevents interaction with the parent view and requires an explicit action to dismiss. Presenting content modally can: Ensure that people receive critical information and, if necessary, act on it.


2 Answers

You could make a UIView with some custom artwork and display it with an animation on top of your view as a "popover" with some buttons like so:

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 100, 50)]; //<- change to where you want it to show.

//Set the customView properties
customView.alpha = 0.0;
customView.layer.cornerRadius = 5;
customView.layer.borderWidth = 1.5f;
customView.layer.masksToBounds = YES;

//Add the customView to the current view
[self.view addSubview:customView];

//Display the customView with animation
[UIView animateWithDuration:0.4 animations:^{
    [customView setAlpha:1.0];
} completion:^(BOOL finished) {}];

Don't forget to #import <QuartzCore/QuartzCore.h>, if you want to use the customView.layer.

like image 111
Aleksander Azizi Avatar answered Nov 09 '22 11:11

Aleksander Azizi


Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.

You can get the class as well as demo project here: https://github.com/soberman/ARSPopover

All you need to do is subclass UIViewController, conform to the UIPopoverPresentationControllerDelegate protocol and set desired modalPresentationStyle along with the delegate value:

// This is your CustomPopoverController.m

@interface CustomPopoverController () <UIPopoverPresentationControllerDelegate>

@end

@implementation CustomPopoverController.m

- (instancetype)init {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationPopover;
        self.popoverPresentationController.delegate = self;
    }
    return self;
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone; //You have to specify this particular value in order to make it work on iPhone.
}

Afterwards, instantiate your newly created subclass in the method from which you want to show it and assign two more values to sourceView and sourceRect. It looks like this:

CustomPopoverController *popoverController = [[CustomPopoverController alloc] init];
popoverController.popoverPresentationController.sourceView = sourceView; //The view containing the anchor rectangle for the popover.
popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 0, 0); //The rectangle in the specified view in which to anchor the popover.
[self presentViewController:popoverController animated:YES completion:nil];

And there you have it, nice, neat blurred popover.

like image 26
Soberman Avatar answered Nov 09 '22 11:11

Soberman