首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有交互式呈现视图的UIPresentationController

具有交互式呈现视图的UIPresentationController
EN

Stack Overflow用户
提问于 2022-04-18 20:05:14
回答 1查看 144关注 0票数 0

我看了这个问题,但没有用:

我正在尝试实现一个工作表表示控制器,类似于UISheetPresentationController for iOS 15,但我也需要它在iOS 14上运行。我也想让它有一个小的停顿,类似于它是如何在地图应用程序中完成的。

因此,我有一个自定义的UIPresentationController类,目前还没有太多,但到目前为止,我拥有的是:

代码语言:javascript
复制
- (CGRect)frameOfPresentedViewInContainerView {
    [super frameOfPresentedViewInContainerView];
    CGRect presentedViewFrame = CGRectZero;
    CGRect containerBounds = self.containerView.bounds;
    presentedViewFrame.size = CGSizeMake(containerBounds.size.width, floor(containerBounds.size.height * 0.5));
    presentedViewFrame.origin = CGPointMake(0, containerBounds.size.height - presentedViewFrame.size.height);
    return presentedViewFrame;
}

- (BOOL)shouldPresentInFullscreen {
    return NO;
}

- (BOOL)shouldRemovePresentersView {
    return NO;
}

这个确实管用。它确实在呈现视图控制器的一半高度上显示视图控制器。问题是呈现视图不再是交互式的,因为有一个视图显然是由表示控制器类添加的。

因此,我的问题是如何使呈现视图具有交互性,在那里我可以滚动它并与按钮和其他控件交互?我希望能够使用表示控制器来表示视图控制器。

EN

回答 1

Stack Overflow用户

发布于 2022-10-27 22:15:36

下面允许您呈现一个较短的模态视图控制器,同时仍然允许与呈现视图控制器进行交互。这并不是为了实现新的UISheetPresentationController的功能。这只解决了能够与两个视图控制器交互的问题,而较短的第二个控制器在视图中。

这种方法使用自定义的UIPresentationController。这避免了处理自定义容器视图和动画显示呈现视图的需要。

从以下自定义UIPresentationController类开始:

代码语言:javascript
复制
import UIKit

class ShortPresentationController: UIPresentationController {
    override var shouldPresentInFullscreen: Bool {
        // We don't want full screen
        return false
    }

    override var frameOfPresentedViewInContainerView: CGRect {
        let size = containerView?.frame.size ?? presentingViewController.view.frame.size

        // Since the containerView's frame has been resized already, we just need to return a frame of the same
        // size with a 0,0 origin.
        return CGRect(origin: .zero, size: size)
    }

    override func presentationTransitionWillBegin() {
        super.presentationTransitionWillBegin()

        guard let containerView = containerView else { return }

        // By default the containerView's frame covers the screen which prevents interacting with the presenting view controller.
        // Update the containerView's frame to match the area needed by the presented view controller. This allows
        // interection with the presenting view controller even while the presented view controller is in view.
        //
        // This code assumes we want the presented view controller to use the full width of the presenting view controller
        // while honoring the preferredContentSize height. It also assumes we want the bottom of the presented view
        // controller to appear at the bottom of the presenting view controller. Adjust as needed.
        let containerSize = containerView.bounds.size
        let preferredSize = presentedViewController.preferredContentSize
        containerView.frame = CGRect(x: 0, y: containerSize.height - preferredSize.height,
                                     width: containerSize.width, height: preferredSize.height)
    }
}

在呈现视图控制器中,需要创建和呈现短视图控制器。这是非常典型的代码,用于呈现一个模态视图控制器,与将样式设置为custom和分配transitioningDelegate的重要区别。

FirstViewController.swift:

代码语言:javascript
复制
let vc = SecondViewController()
let nc = UINavigationController(rootViewController: vc)
nc.modalPresentationStyle = .custom
nc.transitioningDelegate = self
present(nc, animated: true)

您需要在FirstViewController中实现转换委托的一种方法,以返回自定义表示控制器:

代码语言:javascript
复制
extension FirstViewController: UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return ShortPresentationController(presentedViewController: presented, presenting: presenting)
    }
}

最后,确保设置了第二个视图控制器的preferredContentSize属性。一个典型的地方是在viewDidLoad of SecondViewController:

代码语言:javascript
复制
preferredContentSize = CGSize(width: 320, height: 300)

这不包括导航控制器栏(如果有的话)。如果您希望呈现视图控制器设置最终大小(包括条形图),则可以在呈现之前在nc上设置它。这取决于您想要指定的首选大小。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71916528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档