将 viewcontroller 拖动到 swift 中的 tabbarcontroller 时出现黑屏?
black screen visible when dragging viewcontroller to tabbarcontroller in swift?
当我从 TabbarController 向外部呈现时 Viewcontroller 正确呈现并且当我拖动以关闭 ViewController 时也关闭 correctly.but 它使用 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)
关闭示例ViewController 到 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
提前致谢
在
之间插入导航控制器
TabbarViewController-->ExampleViewController
所以应该是:
TabbarViewController--> NavigationController-->ExampleViewController
只需 Select ExampleViewController 选择在故事板中嵌入导航控制器。
并使用 Segue 推送方法。
尝试了您的代码,只需在选项卡屏幕和视图控制器之间提供 NavigationController
。
let newVC = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
newVC.modalPresentationStyle = .overCurrentContext
self.present(newVC, animated: true, completion: nil)
并且在下一个屏幕中使用与您上面提到的相同的功能,没有黑屏。
或者您可以按照此处的建议在 AppDelegate
中使用以下行:
window?.backgroundColor = UIColor.white
当我从 TabbarController 向外部呈现时 Viewcontroller 正确呈现并且当我拖动以关闭 ViewController 时也关闭 correctly.but 它使用 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)
关闭示例ViewController 到 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
提前致谢
在
之间插入导航控制器TabbarViewController-->ExampleViewController
所以应该是:
TabbarViewController--> NavigationController-->ExampleViewController
只需 Select ExampleViewController 选择在故事板中嵌入导航控制器。 并使用 Segue 推送方法。
尝试了您的代码,只需在选项卡屏幕和视图控制器之间提供 NavigationController
。
let newVC = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
newVC.modalPresentationStyle = .overCurrentContext
self.present(newVC, animated: true, completion: nil)
并且在下一个屏幕中使用与您上面提到的相同的功能,没有黑屏。
或者您可以按照此处的建议在 AppDelegate
中使用以下行:
window?.backgroundColor = UIColor.white