从Swift中的任意锚点呈现一个popover [英] Present a popover from an arbitrary anchor point in Swift

查看:552
本文介绍了从Swift中的任意锚点呈现一个popover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何根据



我想添加任意锚点的弹出窗口。我看到的其他SO答案是针对条形按钮项目或在Objective-C中。



我刚刚学会了如何做到这一点,所以我在下面添加我自己的答案。

解决方案

更新为Swift 3



在故事板中,添加您想要成为弹出窗口的视图控制器。将故事板ID设置为popoverId。





还向主视图控制器添加一个按钮,并将IBAction连接到以下代码。

  import UIKit 
class ViewController:UIViewController,UIPopoverPresentationControllerDelegate {

@IBAction func buttonTap(sender:UIButton){

//获取对popover的视图控制器的引用
let popController = UIStoryboard(名称:Main,bundle:nil).instantiateViewController(withIdentifier:popoverId)

//设置演示文稿样式
popController.modalPresentationStyle = UIModalPresentationStyle.popover

//设置popover演示控制器
popController.popoverPresentationController?.permittedArrowDirections = UI PopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender // button
popController.popoverPresentationController?.sourceRect = sender.bounds

//呈现popover
self.present(popController,animated:true,completion:nil)
}

// UIPopoverPresentationControllerDelegate方法
func adaptivePresentationStyleForPresentationController(controller :UIPresentationController) - > UIModalPresentationStyle {
//强制popover样式
返回UIModalPresentationStyle.none
}
}

设置 sourceView sourceRect 是允许您选择要显示的任意点的原因popover。



就是这样。现在按下按钮时它应该是这样的。





感谢这篇文章寻求帮助。


I know how to present a popover from a bar button item as is described in this answer (for both iPhone and iPad).

I would like to add a popover for an arbitrary anchor point. The other SO answers that I saw were for bar button items or in Objective-C.

I just learned how to do this, so I am adding my own answer below.

解决方案

Updated for Swift 3

In the storyboard, add a view controller that you would like to be the popover. Set the Storyboard ID to be "popoverId".

Also add a button to your main view controller and hook up the IBAction to the following code.

import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

    @IBAction func buttonTap(sender: UIButton) {

        // get a reference to the view controller for the popover
        let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverId")

        // set the presentation style
        popController.modalPresentationStyle = UIModalPresentationStyle.popover

        // set up the popover presentation controller
        popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
        popController.popoverPresentationController?.delegate = self
        popController.popoverPresentationController?.sourceView = sender // button
        popController.popoverPresentationController?.sourceRect = sender.bounds

        // present the popover
        self.present(popController, animated: true, completion: nil)
    }

    // UIPopoverPresentationControllerDelegate method
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        // Force popover style
        return UIModalPresentationStyle.none
    }
}

Setting the sourceView and sourceRect is what allows you to choose an arbitrary point to display the popover.

That's it. Now it should like something like this when the button is tapped.

Thanks to this article for help.

这篇关于从Swift中的任意锚点呈现一个popover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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