【问题标题】:Open modal ViewController in window with my sizes用我的尺寸在窗口中打开模态 ViewController
【发布时间】:2018-01-08 18:48:09
【问题描述】:

如何使用我的尺寸在窗口中打开模态 ViewController?

我尝试使用它,但它不起作用

modalViewController?.view.superview?.frame = CGRect(x:162, y: 333, width: 700,height: 700)
//
modalViewController?.view.superview?.center = self.view.center

代码:

let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
    modalViewController?.modalPresentationStyle = .overCurrentContext
    self.present(modalViewController!, animated: true, completion: nil)

示例:

【问题讨论】:

    标签: ios swift modalviewcontroller


    【解决方案1】:

    试试:

    let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
    modalViewController?.modalPresentationStyle = .formSheet
    self.present(modalViewController!, animated: true, completion: nil)
    modalViewController!.preferredContentSize = CGSize(width: 700, height: 700)
    

    【讨论】:

      【解决方案2】:

      由于您使用的是 iPad,因此您可以通过以编程方式或在故事板中设置视图控制器的首选内容大小来实现。
      但我建议你使用presentation controllers。 通过使用它们,您可以返回您喜欢的尺寸或插图。
      这是我的一个代码。

      class PGSHelpPresentationController: UIPresentationController {
          // The dimming view is the view the you have around the the content of the view controller you want to present
          let dimmingView = UIView()
      
          override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
              super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
              // here the dimming view is white with an alpha of 0.5
              dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
          }
      
          override public func presentationTransitionWillBegin() {
              // Here I'm setting the initial properties of the dimming view (alpha and size)
              dimmingView.frame = containerView!.bounds
              dimmingView.alpha = 0.0
              containerView!.insertSubview(dimmingView, at: 0)
              //Here I'm asking to animate the change of alpha along with the transition animation of the view controller 
              presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
                  context in
                  self.dimmingView.alpha = 1.0
                  },
                  completion: nil)
          }
      
          override public func dismissalTransitionWillBegin() { //Here I'm asking that once the dismissal begin I'd like also that the the dimming view refers to sully transparent
              presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
                  context in
                  self.dimmingView.alpha = 0.0
                  },
                  completion: {
                      context in
                      self.dimmingView.removeFromSuperview()
              })
          }
         //Here I'm setting the size of the view controller that it should be displayed with an inset of 20pt
          override public var frameOfPresentedViewInContainerView: CGRect {
              return containerView!.bounds.insetBy(dx: 20, dy: 20)
          }
      
          override public func containerViewWillLayoutSubviews() {
              dimmingView.frame = containerView!.bounds
              presentedView!.frame = frameOfPresentedViewInContainerView
          }
      
      }
      

      现在我可以创建一个自定义动画,这是一个简单的幻灯片过渡:

      class PGSBouncyViewControllerAnimator: NSObject, UIViewControllerAnimatedTransitioning {
          public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
              return 0.8
          }
      
          public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
              if let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) {
                  let centre = presentedView.center
                  presentedView.center = CGPoint(x:centre.x,y: -presentedView.bounds.size.height)
                  transitionContext.containerView.addSubview(presentedView)
                  UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: { () -> Void in
                      presentedView.center = centre
                  }, completion: { _ in
                      transitionContext.completeTransition(true)
      
                  })
      
              }
          }
      }
      

      现在我创建转换委托,该对象通过返回演示控制器和自定义动画来管理转换。

      public class PGSHelpTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
      
          public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
              return PGSHelpPresentationController(presentedViewController: presented,
                                                   presenting: presenting)
          }
      
          public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
              return PGSBouncyViewControllerAnimator()
          }
      
      }
      

      如果您想展示您的自定义视图控制器,只需在展示的视图控制器中创建转换委托作为属性 (lazy var bouncyTransitioningDelegate = PGSHelpTransitioningDelegate() ) 并执行此操作:

      let busyViewController = BusyViewController()
                  busyViewController.transitioningDelegate = bouncyTransitioningDelegate
                  busyViewController.modalPresentationStyle = .custom
      

      【讨论】:

        猜你喜欢
        • 2012-12-02
        • 2013-11-08
        • 1970-01-01
        • 2016-10-08
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 2021-08-29
        • 1970-01-01
        相关资源
        最近更新 更多