【问题标题】:How to update the frameOfPresentedViewInContainerView when using custom modal presentation?使用自定义模态演示时如何更新 frameOfPresentedViewInContainerView?
【发布时间】:2019-11-26 02:23:40
【问题描述】:

我正在使用自定义 UIPresentationController 以模态方式呈现视图。呈现视图后,呈现视图中的第一个文本字段成为第一响应者,并且键盘出现。为了确保视图仍然可见,我将其向上移动。但是,当我这样做时,frameOfPresentedViewInContainerView 不再与视图的实际框架匹配。因此,当我点击视图时,它会被关闭,因为 backgroundView 上有一个 tapGestureRecogziner,它位于 presentingView 的顶部。如何通知presentingControllerpresentingView的frame/position发生了变化?

在 UIPresentationController 中:

    override var frameOfPresentedViewInContainerView: CGRect {
        var frame =  CGRect.zero
        let safeAreaBottom = self.presentingViewController.view.safeAreaInsets.bottom
        guard let height = presentedView?.frame.height else { return frame }
        if let containerBounds = containerView?.bounds {
            frame = CGRect(x: 0,
                           y: containerBounds.height - height - safeAreaBottom,
                           width: containerBounds.width,
                           height: height + safeAreaBottom)
        }
        return frame
    }

    override func presentationTransitionWillBegin() {
        if let containerView = self.containerView, let coordinator = presentingViewController.transitionCoordinator {
            containerView.addSubview(self.dimmedBackgroundView)
            self.dimmedBackgroundView.backgroundColor = .black
            self.dimmedBackgroundView.frame = containerView.bounds
            self.dimmedBackgroundView.alpha = 0
            coordinator.animate(alongsideTransition: { _ in
                self.dimmedBackgroundView.alpha = 0.5
            }, completion: nil)
        }
    }

以模态方式呈现视图:

            let overlayVC = CreateEventViewController()

            overlayVC.transitioningDelegate = self.transitioningDelegate
            overlayVC.modalPresentationStyle = .custom
            self.present(overlayVC, animated: true, completion: nil)

键盘出现时的动画(在呈现的视图中):

    @objc func animateWithKeyboard(notification: NSNotification) {
        let userInfo = notification.userInfo!
        guard let keyboardHeight = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height,
            let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
            let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else {
                return
        }

        // bottomContraint is the constraint that pins content to the bottom of the superview.
        let moveUp = (notification.name == UIResponder.keyboardWillShowNotification)
        bottomConstraint.constant = moveUp ? (keyboardHeight) : originalBottomValue

        let options = UIView.AnimationOptions(rawValue: curve << 16)
        UIView.animate(withDuration: duration, delay: 0,
                       options: options,
                       animations: {
                        self.view.layoutIfNeeded()
        }, completion: nil)
    }

【问题讨论】:

    标签: ios swift uipresentationcontroller uimodalpresentationcustom


    【解决方案1】:

    来自 Apple 文档:

    UIKit 在一个过程中多次调用这个方法 演示文稿,因此您的实现应该返回相同的帧 每次长方形。请勿使用此方法更改您的 查看层次结构或执行其他一次性任务。

    AFAIK,如果您通过此变量指定框架,建议不要在整个演示过程中更改它。如果您打算使用帧,请不要指定此变量并在动画师中手动处理所有更改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2015-03-03
      相关资源
      最近更新 更多