如何在高度较小的当前视图控制器上呈现UIViewController

时间:2018-04-07 19:10:30

标签: ios swift uiviewcontroller

我想在当前视图控制器的顶部呈现一个UIViewController,并将其高度设置为屏幕大小的~80%。我有第一部分:

let additionalVC = ChartsViewController(currentSelection)
additionalVC = .overCurrentContext
present(additionalVC, animated: true)

我尝试在viewDidLoad中的self.view.frame内设置ChartsVC以及几种不同的内容,但它始终以全屏模式显示。

这就是我想要实现的目标:

  • blueVC - currentVC
  • redVC - ChartsVC - 当前VC顶部的VC,原始高度约为80%

enter image description here

btw我正在以编程方式执行所有操作,没有xib和UIStoryboard

4 个答案:

答案 0 :(得分:3)

有很多方法可以实现这一目标。

您可以使用第三方框架(http://transitiontreasury.com/)或我的方式。

呈现newVC,其中transition =模型超过当前上下文

确保newVC.views背景颜色清晰

添加另一个视图,其中origin.y是顶部和所需间隙之间的距离。这是所有物体都在的视图。

如果你需要一个编码示例让我知道,但它是一个非常简单的解决方案,并在那里看你的代码80%。

托马斯

答案 1 :(得分:1)

在IOS 13和Xcode 11中,您可以使用modalPresentationStyle = .automatic来呈现ViewController

答案 2 :(得分:0)

选择两个.noDecoration { text-decoration: none; } 。首先ViewController有一个view controller,按钮操作名称为button。目标是点击我们想要的clickedbutton作为第一个secondVC的孩子添加,并在第一个view controller中显示secondVC 80%。再点击我们从view controller移除secondVC的按钮。以下是first view controller的代码。

click button action

此处@IBAction func clicked(_ sender: UIButton) { if !isshown{ isshown = true self.addChildViewController(vc) self.view.addSubview(vc.view) vc.didMove(toParentViewController: self) let height = view.frame.height let width = view.frame.width UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { self.vc.view.frame = CGRect(x: 0, y: 100 , width: width, height: height - 100) }, completion: { (result) in // do what you want to do }) }else{ isshown = false UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in var frame = self.vc.view.frame frame.origin.y = UIScreen.main.bounds.maxY self.vc.view.frame = frame }, completion: { (finished) -> Void in self.vc.view.removeFromSuperview() self.vc.removeFromParentViewController() }) } } vc的引用。

secondVC

更改下面的代码以获得您想要的百分比

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Second") as! secondVC

答案 3 :(得分:0)

实施自定义UIPresentationController。要使用自定义视图大小,您只需覆盖单个属性。

此代码将简单地将呈现的视图控制器插入50x100点:

class MyPresentationController: UIPresentationController {

    // Inset by 50 x 100
    override var frameOfPresentedViewInContainerView: CGRect {
        return self.presentingViewController.view.bounds.insetBy(dx: 50, dy: 100)
    }

}

要使呈现视图控制器变暗,请覆盖presentationTransitionWillBegin()dismissalTransitionWillBegin()以插入着色视图并将其设置为视图中的动画:

class MyPresentationController: UIPresentationController {

    override var frameOfPresentedViewInContainerView: CGRect {
        return self.presentingViewController.view.bounds.insetBy(dx: 50, dy: 100)
    }

    let shadeView = UIView()

    override func presentationTransitionWillBegin() {
        self.shadeView.backgroundColor = UIColor.black
        self.shadeView.alpha = 0

        // Insert the shade view above the presenting view controller
        self.shadeView.frame = self.presentingViewController.view.frame
        self.containerView?.insertSubview(shadeView, 
            aboveSubview: self.presentingViewController.view)

        // Animate it into view
        self.presentingViewController.transitionCoordinator?.animate(alongsideTransition: { (context) in
            self.shadeView.alpha = 0.3
        }, completion: nil)
    }

    override func dismissalTransitionWillBegin() {
        self.presentingViewController.transitionCoordinator?.animate(alongsideTransition: { (context) in
            self.shadeView.alpha = 0.0
        }, completion: nil)
    }
}

要使用自定义演示文稿控制器,请设置modalPresentationStyletransitioningDelegate

class MyViewController : UIViewController, UIViewControllerTransitioningDelegate {

  // 
  // Your code
  //
  func presentCharts() {
    let additionalVC = ChartsViewController(currentSelection)
    additionalVC.modalPresentationStyle = .custom
    additionalVC.transitioningDelegate = self
    self.present(additionalVC, animated: true)
  }

  //
  // UIViewControllerTransitioningDelegate protocol
  //
  func presentationController(forPresented presented: UIViewController,
                              presenting: UIViewController?,
                              source: UIViewController) -> UIPresentationController? {

    return MyPresentationController(presentedViewController: presented,
                                    presenting: presenting)

  }
}