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.

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

Simulated MetricsUnder “Simulated Metrics” change “Size” to “Freeform” and “Status Bar” to “None.”

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.

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:

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:

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:

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

Hello, there!

That’s it.

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文加上进行如下截图做的设置即可,同上述

173432_RlhV_1443665.png

173433_x6Kk_1443665.png

运行截图如下:

173233_f0Wx_1443665.png

如果不想自己实现,可以直接用现成的第三方库,下面推荐一个

https://github.com/nicolaschengdev/WYPopoverController


转载于:https://my.oschina.net/sayonala/blog/533888

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值