【问题标题】:black screen visible when dragging viewcontroller to tabbarcontroller in swift?在swift中将viewcontroller拖到tabbarcontroller时黑屏可见?
【发布时间】:2020-06-18 03:29:57
【问题描述】:

当我从 TabbarController 呈现到外部 Viewcontroller 正确呈现并且正确关闭时。但是当我拖动以关闭 ViewController 时,它使用 UIPanGestureRecognizer 显示黑屏。

从 TabbarController 到 ViewController 当前代码

  let newVC = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
  self.definesPresentationContext = true
  newVC.modalPresentationStyle = .overCurrentContext
  self.present(newVC, animated: true, completion: nil)

Dismiss ExampleViewController 到 TabbarController 代码

override func viewDidLoad()
{
    super.viewDidLoad()

     let gestureRecognizer = UIPanGestureRecognizer(target: self,
                                                   action: #selector(panGestureRecognizerHandler(_:)))
    view.addGestureRecognizer(gestureRecognizer)

}

@IBAction func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {
    let touchPoint = sender.location(in: view?.window)
    var initialTouchPoint = CGPoint.zero

    switch sender.state {
    case .began:
        initialTouchPoint = touchPoint

    case .changed:
        if touchPoint.y > initialTouchPoint.y {
            view.frame.origin.y = touchPoint.y - initialTouchPoint.y

        }
    case .ended, .cancelled:
        if touchPoint.y - initialTouchPoint.y > 200 {

            self.navigationController?.popViewController(animated: false)
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                self.view.frame = CGRect(x: 0,
                                         y: 0,
                                         width: self.view.frame.size.width,
                                         height: self.view.frame.size.height)

            })
        }
    case .failed, .possible:
        break
    @unknown default:
        break
    }
}

注意:- 我的项目层次结构像这样 NavigationController --> SomeViewControllers -->TabbarViewController-->ExampleViewController

提前致谢

【问题讨论】:

  • 你能分享你的用户界面截图吗?
  • 你的意思是我的 UI 中的 ExampleViewController ?
  • 这不是如何拖动视图控制器的视图。您需要一个自定义的交互式过渡动画。
  • 删除此行:newVC.modalPresentationStyle = .overCurrentContext 并再次尝试拖动。
  • @Niraj,我也删除了这一行,但无法正常工作。我的代码中还有其他要更改的地方吗?

标签: ios swift uitabbarcontroller tabbarcontroller dismissviewcontroller


【解决方案1】:

尝试了您的代码,只需在选项卡屏幕和视图控制器之间提供NavigationController

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

在下一个屏幕中使用了与上面提到的相同的功能,没有黑屏。

或者您可以按照此处的建议在AppDelegate 中使用以下行:https://stackoverflow.com/a/45994837/12830762

window?.backgroundColor = UIColor.white

【讨论】:

  • 我尝试了你所说的但仍然是相同的黑屏。我想在 UI 或代码中更改什么?
  • 当我尝试使用 Viewcontroller 到 ExampleViewController 它工作正常
  • 实际上当将 ViewController 关闭到 ViewController 时工作良好,没有黑屏。但是当 ViewController 到 TabbarController 不工作时,它显示黑屏。当拖动关闭时
  • 我更新了我的 UI 检查它
  • 您的 UI 看起来很完美。您从哪里展示您的ExampleViewController 标签栏或标签栏的第一个视图控制器?
【解决方案2】:

在中间插入导航控制器

TabbarViewController-->ExampleViewController

应该是这样的:

TabbarViewController--> NavigationController -->ExampleViewController

只需选择 ExampleViewController 选择在情节提要中嵌入导航控制器。 并使用 Segue 推送方法。

【讨论】:

  • 我不想使用 Segue 方法。而且没有 NavigationController 到 ExampleViewController 它对我来说很好。
  • 您也可以将 present 与导航控制器一起使用,并在不需要时隐藏导航栏。
  • 我也试过这个让 newVC : AddActivityVC = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController let navigationController = UINavigationController(rootViewController: newVC) self.navigationController?.pushViewController(newVC, animated: true)
  • 但不工作,我用 present 代替 push 保持不变
  • 这样使用:让 exampleViewController = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController 让 navigationController = UINavigationController(rootViewController: exampleViewController) navigationController.modalPresentationStyle = UIModalPresentationStyle。 overCurrentContext self.present(navigationController, 动画: true, 完成: nil)
猜你喜欢
  • 2015-04-17
  • 2017-10-22
  • 2016-03-03
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多