Swift - iOS 8中的UIPopoverController [英] Swift - UIPopoverController in iOS 8

查看:245
本文介绍了Swift - iOS 8中的UIPopoverController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个简单的popoverController添加到我的iphone应用程序中,而我正在努力使用经典的空白屏幕,当我点击按钮时,它会覆盖所有内容。

I'm trying to add a simple popoverController to my iphone app, and I'm currently struggling with the classic "blank screen" which covers everything when I tap the button.

我的代码如下所示:

@IBAction func sendTapped(sender: UIBarButtonItem) {


    var popView = PopViewController(nibName: "PopView", bundle: nil)

    var popController = UIPopoverController(contentViewController: popView)

    popController.popoverContentSize = CGSize(width: 3, height: 3)

    popController.presentPopoverFromBarButtonItem(sendTappedOutl, permittedArrowDirections: UIPopoverArrowDirection.Up, animated: true)

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
        // Return no adaptive presentation style, use default presentation behaviour
        return .None
    }

}

adaptivePresentationStyleForPresentationController函数只是我添加的内容,因为我在某处读到了这是你需要实现的,以便在iphone上获得此功能。但仍然:整个屏幕仍然有一个空白图像,我不知道如何解决它。

The adaptivePresentationStyleForPresentationController-function was just something I added because I read somewhere that this is what you need to implement to get this function on the iphone. But still: there is still a blank image covering the whole screen, and I do not know how to fix it.

任何建议都将受到赞赏。

Any suggestions would be appreciated.

推荐答案

我为此实施的解决方案基于2014年WWDC会议中提供的示例查看iOS 8中的控制器进度(参见幻灯片笔记)。请注意,您必须实现 adaptivePresentationStyleForPresentationController 函数作为 UIPopoverPresentationControllerDelegate 的一部分,但该函数应该在您的主视图控制器中的 sendTapped 函数,您必须在该文件的类声明行中指定 UIPopoverPresentationControllerDelegate 确保您的代码修改了该行为。我还冒昧地将逻辑分离出来,将弹出窗口中的视图控制器呈现给它自己的函数,并添加了一个检查,以确保该函数在当前上下文中已经呈现时不会呈现请求视图控制器。

The solution I implemented for this is based on an example presented in the 2014 WWDC session View Controller Advancements in iOS 8 (see the slide notes). Note that you do have to implement the adaptivePresentationStyleForPresentationController function as a part of the UIPopoverPresentationControllerDelegate, but that function should be outside of your sendTapped function in your main view controller, and you must specify UIPopoverPresentationControllerDelegate in your class declaration line in that file to make sure that your code modifies that behaviour. I also took the liberty to separate out the logic to present a view controller in a popover into its own function and added a check to make sure the function does not present the request view controller if it is already presented in the current context.

所以,你的解决方案看起来像这样:

So, your solution could look something like this:

// ViewController must implement UIPopoverPresentationControllerDelegate
class TheViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    // ...
    // The contents of TheViewController class
    // ...

    @IBAction func sendTapped(sender: UIBarButtonItem) {
        let popView = PopViewController(nibName: "PopView", bundle: nil)
        self.presentViewControllerAsPopover(popView, barButtonItem: sender)
    }

    func presentViewControllerAsPopover(viewController: UIViewController, barButtonItem: UIBarButtonItem) {
        if let presentedVC = self.presentedViewController {
            if presentedVC.nibName == viewController.nibName {
                // The view is already being presented
                return
            }
        }
        // Specify presentation style first (makes the popoverPresentationController property available)
        viewController.modalPresentationStyle = .Popover
        let viewPresentationController = viewController.popoverPresentationController?
        if let presentationController = viewPresentationController {
            presentationController.delegate                 = self
            presentationController.barButtonItem            = barButtonItem
            presentationController.permittedArrowDirections = .Up
        }
        viewController.preferredContentSize = CGSize(width: 30, height: 30)

        self.presentViewController(viewController, animated: true, completion: nil)
    }

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



真实世界实现



我在Github上托管的正在进行的应用程序中的注册表单上实现了此方法的输入验证。我在 UIVIewController 的扩展。相对= nofollow> 的UIViewController + Extensions.swift 。您可以在 <$ c $的验证功能中看到它正在使用中C> AuthViewController.swift presentAlertPopover 方法获取一个字符串并使用它来设置我已设置的 GenericAlertViewController 中的UILabel的值(使得动态文本弹出窗口变得容易) )。但实际的popover魔法都发生在 presentViewControllerAsPopover 方法,它有两个参数:要显示的 UIViewController 实例,以及 UIView 要用作弹出窗口的锚点的对象。箭头方向硬编码为 UIPopoverArrowDirection.Up ,但这并不难改变。

Real world implementation

I implemented this approach for input validation on a sign up form in an in-progress app that I host on Github. I implemented it as extensions to UIVIewController in UIViewController+Extensions.swift. You can see it in use in the validation functions in AuthViewController.swift. The presentAlertPopover method takes a string and uses it to set the value of a UILabel in a GenericAlertViewController that I have set up (makes it easy to have dynamic text popovers). But the actual popover magic all happens in the presentViewControllerAsPopover method, which takes two parameters: the UIViewController instance to be presented, and a UIView object to use as the anchor from which to present the popover. The arrow direction is hardcoded as UIPopoverArrowDirection.Up, but that wouldn’t be hard to change.

这篇关于Swift - iOS 8中的UIPopoverController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆