我正在尝试使用panGesture将secondViewController从firstViewController的顶部移开,但是我遇到了问题...
当我开始移动secondViewController时,控制器后面将出现一个黑屏,而不是firstViewController(如我所料),并且我不知道如何解决此问题...
这是说明我的问题的代码...
firstVC在这里。
import UIKit
class mainController: UIViewController {
let tapGestureRecognizer = UITapGestureRecognizer()
let secondVC = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
tapGestureRecognizer.addTarget(self, action: #selector(goToSecondVC))
view.backgroundColor = UIColor.red
view.addGestureRecognizer(tapGestureRecognizer)
}
@objc func goToSecondVC(){
present(secondVC, animated: true, completion: nil)
}
}
和第二个VC在这里。
import UIKit
class SecondViewController: UIViewController {
let panGesture = UIPanGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
panGesture.addTarget(self, action: #selector(moveView))
view.addGestureRecognizer(panGesture)
}
@objc func moveView(pan: UIPanGestureRecognizer){
let translation = pan.translation(in: view)
if pan.state == .began{
print("began")
} else if pan.state == .changed {
view.frame.origin.x += translation.x
pan.setTranslation(CGPoint.zero, in: view)
} else if pan.state == .ended{
print("ended")
}
}
}
为什么第二个viewController之后的第二个视图控制器后面有黑屏?
答案 0 :(得分:1)
您只需要正确设置SecondViewController
的{{1}} ...
modalPresentationStyle