【问题标题】:Changing the size of a modal view controller更改模态视图控制器的大小
【发布时间】:2019-07-11 06:46:56
【问题描述】:

一旦用户点击一个按钮,我希望我的 modalViewController 在屏幕中间显示为一个小方块(您仍然可以在后台看到原始视图控制器)。

我在 stackoverflow 上找到的几乎所有答案都使用情节提要来创建模态视图控制器,但我已经找到了一切。

当你点击应该调出模态视图的按钮时,这个函数被调用:

func didTapButton() {
    let modalViewController = ModalViewController()
    modalViewController.definesPresentationContext = true
    modalViewController.modalPresentationStyle = .overCurrentContext
    navigationController?.present(modalViewController, animated: true, completion: nil)
}

modalViewController 包含:

import UIKit

class ModalViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .blue
        view.isOpaque = false

        self.preferredContentSize = CGSize(width: 100, height: 100)

    }

}

根据我找到的答案,我的印象是,如果我设置preferredContentSize = CGSize(width: 100, height: 100),那么它会使模态视图控制器为 100px x 100px。

但是,视图控制器占据了整个屏幕(除了标签栏,因为我设置了modalViewController.modalPresentationStyle = .overCurrentContext

我显然在这里遗漏了一步,但我想以编程方式完成所有事情,因为我的项目中根本没有使用情节提要(除了设置打开控制器)

提前感谢您的帮助!!

【问题讨论】:

  • 您需要使用自定义演示控制器。此处示例:github.com/mattneub/custom-alert-view-iOS7
  • 是的,你必须覆盖 frameOfPresentedViewInContainerView 才能返回你想要的 CGRect。这需要从 UIPresentationController 创建一个自定义展示控制器

标签: ios swift uiviewcontroller modalviewcontroller


【解决方案1】:

modalPresentationStyledocumentation 告诉我们

在水平紧凑的环境中,模态视图控制器总是全屏显示。

因此,如果您想在 iPhone 中以纵向模式执行此操作,您必须指定 .custom 演示样式并让您的过渡委托提供自定义演示控制器。

我个人会让我的第二个视图控制器管理它自己的呈现参数,所以我的第一个视图控制器可能只:

class FirstViewController: UIViewController {
    @IBAction func didTapButton(_ sender: Any) {
        let controller = storyboard!.instantiateViewController(withIdentifier: "SecondViewController")
        present(controller, animated: true)
    }
}

然后我的第二个视图控制器将指定一个自定义转换并指定一个自定义转换委托:

class SecondViewController: UIViewController {
    private var customTransitioningDelegate = TransitioningDelegate()

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configure()
    }
}

private extension SecondViewController {
    func configure() {
        modalPresentationStyle = .custom
        modalTransitionStyle = .crossDissolve              // use whatever transition you want
        transitioningDelegate = customTransitioningDelegate
    }
}

然后,过渡委托将出售自定义表示控制器:

class TransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return PresentationController(presentedViewController: presented, presenting: presenting)
    }
}

并且那个展示控制器会指定它的大小:

class PresentationController: UIPresentationController {
    override var frameOfPresentedViewInContainerView: CGRect {
        let bounds = presentingViewController.view.bounds
        let size = CGSize(width: 200, height: 100)
        let origin = CGPoint(x: bounds.midX - size.width / 2, y: bounds.midY - size.height / 2)
        return CGRect(origin: origin, size: size)
    }

    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)

        presentedView?.autoresizingMask = [
            .flexibleTopMargin,
            .flexibleBottomMargin,
            .flexibleLeftMargin,
            .flexibleRightMargin
        ]

        presentedView?.translatesAutoresizingMaskIntoConstraints = true
    }
}

这只是自定义过渡的冰山一角。您可以指定动画控制器(用于自定义动画)、暗淡/模糊背景等。请参阅 WWDC 2013 Custom Transitions Using View Controllers 视频了解自定义转换的入门,以及 WWDC 2014 视频 View Controller Advancements in iOS 8A Look Inside Presentation Controllers 深入了解演示控制器。


例如,您可能希望在呈现模态视图时使背景变暗和模糊。因此,您可以添加 presentationTransitionWillBegindismissalTransitionWillBegin 以动画显示这个“变暗”视图:

class PresentationController: UIPresentationController {
    ...

    let dimmingView: UIView = {
        let dimmingView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
        dimmingView.translatesAutoresizingMaskIntoConstraints = false
        return dimmingView
    }()

    override func presentationTransitionWillBegin() {
        super.presentationTransitionWillBegin()

        let superview = presentingViewController.view!
        superview.addSubview(dimmingView)
        NSLayoutConstraint.activate([
            dimmingView.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
            dimmingView.trailingAnchor.constraint(equalTo: superview.trailingAnchor),
            dimmingView.bottomAnchor.constraint(equalTo: superview.bottomAnchor),
            dimmingView.topAnchor.constraint(equalTo: superview.topAnchor)
        ])

        dimmingView.alpha = 0
        presentingViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
            self.dimmingView.alpha = 1
        }, completion: nil)
    }

    override func dismissalTransitionWillBegin() {
        super.dismissalTransitionWillBegin()

        presentingViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
            self.dimmingView.alpha = 0
        }, completion: { _ in
            self.dimmingView.removeFromSuperview()
        })
    }
}

产生:

【讨论】:

  • 如何在使用 UIPresentationController 呈现后更改呈现的视图控制器大小?我想在呈现的视图控制器中放置一个UIButton,当点击时,我想更改呈现的视图控制器的大小。
【解决方案2】:

可以将视图控制器的背景色设置为清除,然后在视图控制器中间创建一个视图,并将模态呈现样式设置为.overCurrentContext,这样你就可以从后面看到视图控制器了。

这是编辑后的示例:

func didTapButton() {
    let modalViewController = storyboard?.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
    modalViewController.modalPresentationStyle = .overCurrentContext
    modalViewController.modalTransitionStyle = .crossDissolve // this will look more natural for this situation
    navigationController?.present(modalViewController, animated: true, completion: nil)
}

这是你展示的视图控制器类:

import UIKit

class ModalViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        createTheView()
    }

    private func createTheView() {

        let xCoord = self.view.bounds.width / 2 - 50
        let yCoord = self.view.bounds.height / 2 - 50

        let centeredView = UIView(frame: CGRect(x: xCoord, y: yCoord, width: 100, height: 100))
        centeredView.backgroundColor = .blue
        self.view.addSubview(centeredView)
    }
}

您已经可以从这里构建:为“更小”的视图控制器添加您想要的外观:)

【讨论】:

  • FWIW,虽然自定义动画和演示控制器为我们提供了对自定义过渡的最终控制权,但您的答案正是我在生产应用程序中所做的。这是一个很好,简单的方法!如果您是这样滚动的,您也可以完全在情节提要中完成。
  • 这是一种非常简单的方法。它非常好用,甚至适用于 iOS 13 Page Sheet!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
相关资源
最近更新 更多