文档章节

iPhone上使用原生ViewController实现Popover

小春0618
 小春0618
发布于 2015/11/22 17:10
字数 508
阅读 4K
收藏 1

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

运行截图如下:

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

https://github.com/nicolaschengdev/WYPopoverController


© 著作权归作者所有

小春0618
粉丝 8
博文 40
码字总数 10552
作品 0
海淀
程序员
私信 提问
加载中

评论(0)

iOS开发捷径学习(三)

Storyboard的segue Storyboard中的segue功能强大,是页面跳转与交互的利器。现在就了解下吧。 初始化 segue的三个参数: identifier:唯一标识,用于标识自己 sourceViewController:来源控制...

智小融
2018/01/02
0
0
iPad开发:UISplitViewController应用

iPad 应用中,UISplitViewController是最具特色的视图控制器,用于显示一个从左右分隔的分栏视图。 新建 View–based Appliation项目:SplitVCDemo。用IB打开SplitVCDemoViewController.xib...

鉴客
2012/01/09
3.9K
0
UIPopoverController 使用总结

先看他的继承关系,UIPopoverController是直接继承自NSObject,它和UIViewController没有关系.那它是怎么实现弹出在所有View之上的,我猜测是利用了keywindow,把这个View加在keywindow里面,...

木木情深
2014/02/18
100
0
【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记44 Popover Segue

在上一话中我们看了一个Demo,使用modal的方式,但其实如果要考虑到适配Iphone和Ipad设备的话,默认的modal会充满屏幕,而popover可以只占据屏幕上的一小块,之前我们讲过在Iphone中使用pop...

cg1991130
2015/08/28
0
0
StoryBoard内动态加入UIViewController

StoryBoard是IOS5的新特性之一,把程序View的设计弄成了绘画板(storyboard),它让枯燥程序猿们体验了下做一次画家。如果是第一次接触StoryBoard,想用StoryBoard做一个最简单的IOS程序,我建...

长平狐
2012/11/01
844
0

没有更多内容

加载失败,请刷新页面

加载更多

Mysql 查询天、周,月,季度、年的数据

今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1 近7天 SELECT * FROM 表名 where DA......

sp_lucky
35分钟前
23
0
java函数式编程Lambda基础 几Stream

java函数式编程Lambda基础 LambdaJava 8引入了Lambda语法,用以简化单抽象方法接口的实现 Lambda语法实现了类型自动推断 只定义了单个(只有一个抽象方法)抽象方法接口被称为函数式接口,标注...

ossea
37分钟前
20
0
异步爬虫+asyncio+python3.7+(async + await )

import asyncio import json import time import traceback import aiohttp import logging from aiohttp import ContentTypeError from motor.motor_asyncio import AsyncIOMotorClient log......

丁典
41分钟前
12
0
项目讲解4

分组取topN的方法: 数据的预处理为使用mapreduce 每日新访客: 回头/单次访客统计: 漏斗模型: 使用python产生数据 1.统计每个步骤的总访问人数 create table tmp_page_views like ods_web...

七宝1
今天
79
0
笔记:pyecharts可视化

# 加载数据import pandas as pddf = pd.read_excel(r"D:\我的文档\数据源\采购商品出库综合查询 - Python.xlsx",header = 2)df # 计算出结果df1 = df.groupby(["部门"])["总销售额"......

tengyulong
今天
113
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部