gpt4 book ai didi

ios - 在 Swift 中从 UIButton 创建弹出窗口

转载 作者:IT王子 更新时间:2023-10-29 05:21:04 24 4
gpt4 key购买 nike

我想从 UIButton 创建一个大约 50x50px 的小弹出窗口。我已经看到使用自适应 segue 的方法,但我的尺寸等级已关闭,这意味着我无法使用此功能!

我还能如何创建这个弹出窗口?我可以使用我的按钮 IBACtion 中的代码创建它吗?或者还有什么方法可以用 Storyboard做到这一点吗?

最佳答案

您可以执行以下两个选项之一:

  • 在您的 UIViewController 中为 UIButton 创建一个 Action ,并在里面呈现您想要的 ViewController 就像一个 Popover 和您的 UIViewController 必须实现协议(protocol) UIPopoverPresentationControllerDelegate,看看下面的代码:

    @IBAction func showPopover(sender: AnyObject) {

    var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdentifier") as! UIViewController

    popoverContent.modalPresentationStyle = .Popover
    var popover = popoverContent.popoverPresentationController

    if let popover = popoverContent.popoverPresentationController {

    let viewForSource = sender as! UIView
    popover.sourceView = viewForSource

    // the position of the popover where it's showed
    popover.sourceRect = viewForSource.bounds

    // the size you want to display
    popoverContent.preferredContentSize = CGSizeMake(200,500)
    popover.delegate = self
    }

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

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

    根据@matt Programming iOS 8 的书:

    A popover presentation controller, in iOS 8, is a presentation controller (UIPresentationController), and presentation controllers are adaptive. This means that, by default, in a horizontally compact environment (i.e. on an iPhone), the .Popover modal presentation style will be treated as .FullScreen. What appears as a popover on the iPad will appear as a fullscreen presented view on the iPhone, completely replacing the interface.

    要在 iPhone 中避免这种行为,您需要实现委托(delegate)方法 adaptivePresentationStyleForPresentationController在您的 UIViewController 中正确显示 Popover。

  • 我认为另一种方法更容易做到,它是使用 Interface Builder,只需从 UIButton 安排到 ViewController你想要的,并在 segue 中选择 Popover segue。

希望对你有帮助

关于ios - 在 Swift 中从 UIButton 创建弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29132940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com