【问题标题】:How to present ViewController in full screen with behavior of popover? Swift如何通过弹出框的行为全屏显示 ViewController?迅速
【发布时间】:2020-12-06 13:49:25
【问题描述】:

我需要全屏显示 VC,但能够在弹出窗口时向下滑动它,类似于它在 Apple Music 中歌曲细节的工作方式。

我已经尝试了 vc.modalPresentationStyle 中的所有选项,但它要么全屏,无法向下滑动关闭,要么具有此功能,但不能全屏

    vc.modalPresentationStyle = .overFullScreen
    vc.modalTransitionStyle = .coverVertical

【问题讨论】:

    标签: swift uiviewcontroller uimodalpresentationstyle


    【解决方案1】:
    1. 设置modalPresentationStyle:
    viewController.modalPresentationStyle = .formSheet
    
    1. 设置preferredContentSize:
    viewController.preferredContentSize = CGSize(width: kScreenWidth, height: kScreenHeight)
    

    【讨论】:

    • 不幸的是,.preferredContentSize 没有帮助。它只是不会改变任何我输入的高度值
    • 是的,preferredContentSize 用于 iPad 屏幕 :)
    【解决方案2】:

    你可以试试这个:

    viewController.modalPresentationStyle = .fullScreen
    

    【讨论】:

      【解决方案3】:

      所以,我只是将它全屏呈现并编写了函数,该函数允许平移关闭vc,并降低alpha

      func panToCloseGesture() {
              let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
              view.addGestureRecognizer(panGesture)
          }
          
          @objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
              let percentThreshold:CGFloat = 0.3
              let translation = sender.translation(in: view)
              
              let newY = self.ensureRange(value: view.frame.minY + translation.y, minimum: 0, maximum: view.frame.maxY)
              let progress = self.progressAlongAxis(newY, view.bounds.height)
              
              view.frame.origin.y = newY
              if newY > 0 {
                  self.rootView.alpha = 1 - (newY / 400)
              }
              
               if sender.state == .ended {
                  let velocity = sender.velocity(in: view)
                  if velocity.y >= 1000 || progress > percentThreshold {
                      self.dismiss(animated: true, completion: nil)
                  } else {
                      UIView.animate(withDuration: 0.2, animations: {
                          self.rootView.alpha = 1
                          self.view.frame.origin.y = 0
                      })
                  }
              }
              sender.setTranslation(.zero, in: view)
          }
      

      【讨论】:

        猜你喜欢
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-02
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多