iPhone上使用原生ViewController實現Popover

Xcode has a built-in adaptive segue called 「Present As Popover」 but by default it only behaves the way you’d expect it to — presenting a view in a speech-bubble style floating popover — on the iPad. On the iPhone your views are presented modally, full-screen. This is on purpose and is yet another subtle encouragement from Apple that we should build our apps with adaptivity in mind, but it’s almost never what I actually want my popovers to do. Here’s how to fix that.html

Drag a new view controller onto your storyboard and tweak these settings:git

Simulated MetricsUnder 「Simulated Metrics」 change 「Size」 to 「Freeform」 and 「Status Bar」 to 「None.」github

Simulated SizeUnder 「Simulated Size」 change your view’s height and width to the actual size you want your popover’s content to be.網絡

Back on the attributes inspector check the box next to 「Use Preferred Explicit Size」 and make sure the height and width match the sizes you set in the previous step.app

Drag a new segue from a control on your presenting view controller (a UIBarButtonItem is a good fit) to your popover view controller. Choose 「popover presentation」 from the dropdown and then give your segue an ID:ide

Segue ID

Open your view controller class file and add the UIPopoverPresentationControllerDelegate protocol:測試

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

Override the prepareForSegue function to catch your popover segue. Set the modalPresentationStyle to .Popover to explicitly state that you want a popover and then assign the delegate property of the view’s popoverPresentationController to self:ui

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "popoverSegue" {
        let popoverViewController = segue.destinationViewController as! UIViewController                     popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover             
        popoverViewController.popoverPresentationController!.delegate = self
    }
}

Finally, implement the adaptivePresentationStyleForPresentationController function to tell your app that you really want that popover presentation and will accept no substitutions:spa

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {         
    return UIModalPresentationStyle.None     
}

Hello, there!

That’s it.code

Edited 4/24/15: Changed an as to an as! for Swift 1.2

上述內容引用地址:http://richardallen.me/2014/11/28/popovers.html

上面內容爲從網絡獲取,以後本身使用OC測試是能夠實現的,我的測試使用xib實現 ,下面將我的源碼貼一下 

主viewcontroller.m

#import "ViewController.h"
#import "PopoverViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"popoverSegue"]) {
        PopoverViewController *pop = segue.destinationViewController;
        pop.modalPresentationStyle = UIModalPresentationPopover;
        pop.popoverPresentationController.delegate = self;
    }
}

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

其中PopoverViewController爲popover顯示的controller,這裏面什麼也不做用 ,只要在PopoverViewController的xib文加上進行以下截圖作的設置便可,同上述

運行截圖以下:

若是不想本身實現,能夠直接用現成的第三方庫,下面推薦一個

https://github.com/nicolaschengdev/WYPopoverController

相關文章
相關標籤/搜索