15

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Xtrician
  • 504
  • 3
  • 5
  • 14

2 Answers2

26

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.

Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
  • If i want to add buttons in it, the problem is all the buttons are stays in one place, you have an idea how i can to place the buttons side by side? – Xtrician Jul 19 '12 at 11:16
  • 1
    `[yourButton setFrame:CGRectMake(5 /*X*/, 2 /*Y*/, 10 /*Width*/, 20 /*Hight*/)];` `// change the X(5) Y(2) for placement` – Aleksander Azizi Jul 19 '12 at 17:35
  • 8
    @AleksanderAzizi: where is the code that makes the triangle shape at the top left of the 'popover' view that you created? – BigSauce Nov 27 '12 at 15:14
  • I have created a custom view with a button, when I click the button, the custom view should be dismissed and the previous view controller should be displayed – chandru Dec 18 '13 at 07:06
16

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.

Soberman
  • 2,556
  • 1
  • 19
  • 22
  • One remark: `self.modalPresentationStyle = UIModalPresentationPopover;` should be first, because in other case `popoverPresentationController` will be nil – Vitalii Gozhenko Jan 29 '16 at 17:32
  • 1
    Marvelous. The API has evolved since the original question, now this should be the accepted answer. – Martin Jun 13 '16 at 14:02