Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popover view for iPhone using XCode 5

I wanted to reuse the popover for iPhone described in this video which is exactly what I need.

The problem is that I couldn't bind a UIViewController property to the popover's UIViewController like in the video.

One difference with the video is that it has been made using XCode 4.2 and I'm using XCode 5.

So the question is: How to make a popover for iPhone like in the video on XCode 5?

Here is the XCode 5 project I am struggling with.

like image 320
Sèb Avatar asked Oct 08 '13 03:10

Sèb


People also ask

What is a popover iOS?

A popover is a transient view that appears above other content onscreen when you tap a control or in an area. Typically, a popover includes an arrow pointing to the location from which it emerged.

How do I present Viewcontroller in popover Swift?

In the Bar Button Item section Change the System Item to Action. Next, Drag a new View Controller from the Object Library on to the Storyboard. Drag a Text View from the Object Library at the top of this View Controller. This View Controller will be displayed in the popover.

How do I show popups in Swift?

1) The @State var showingPopup variable will control the displaying of the popup. 2) The only button on the screen will change the showingPopup variable state. 3) We add the popup as a modifier of our view, passing a binding for the showingPopup to control the state inside the popup implementation.

What is Popoverpresentationcontroller in Swift?

The nearest popover presentation controller that is managing the current view controller.


1 Answers

I figured out a way to get popover to work on iPhone and iPad programmatically !

  • Create a category to make popover available on iPhone (more details here)

    //UIPopover+Iphone.h
    @interface UIPopoverController (overrides)
    + (BOOL)_popoversDisabled;
    @end
    
    //UIPopover+Iphone.m
    @implementation UIPopoverController (overrides)
    + (BOOL)_popoversDisabled { return NO;
    }
    @end
    
  • Create the button which will show the popover and implement the method it calls

ExampleUIViewController.h

@interface ExampleViewController : UIViewController <UIPopoverControllerDelegate>
    @property (strong, nonatomic) UIButton *detailButton;
    @property (nonatomic, retain) IBOutlet UIPopoverController *poc;

UIPopoverController poc has to be held in an instance variable, more details here.

ExampleUIViewController.m

- (void)viewDidLoad {
    _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_detailButton addTarget:self
                action:@selector(showPop:)
      forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_detailButton];
}

-(void)showPop:(UIButton *)button {
   UIViewController *detailsViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
   self.poc = [[UIPopoverController alloc] initWithContentViewController:detailsViewController];
   [self.poc setDelegate:self];
   [self.poc presentPopoverFromRect:_detailButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
  • Create the UIViewController that will contain what's displayed inside the popover (called DetailsViewController in the example)

Simply create it in your project by a right click -> New File -> Objective c class -> UIViewController and tick the box "With XIB".

Then a popover will appear right next to the button when tapped.

Tested OK on iOs5 and above.

like image 94
Sèb Avatar answered Nov 15 '22 08:11

Sèb