大家好我关闭我的NavigationController有问题。 这段代码对我不起作用。当我尝试在我的ViewController中调用它时
@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
self.navigationController?.dismiss(animated: true, completion: nil)
}
这就是我在第一个ViewController中所做的事情我只是打开了具有自定义转换的ProfileViewController。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Profile") as? NavigationControllerTransition
controller?.modalPresentationStyle = .custom
controller?.transitioningDelegate = self
controller?.interactor = self.interactor
controller?.animatorDirection.direction = .right
self.animatedTransition.animatorDirection.direction = .right
self.present(controller!, animated: true, completion: {})
我有一个自定义的NavigationController类,它调用NavigationControllerTransition使其碰巧从屏幕边缘滑动以进行自定义转换。
class NavigationControllerTransition: UINavigationController {
var interactor: Interactor?
var animatorDirection = AnimatorDirection()
override func viewDidLoad() {
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(NavigationControllerTransition.handleTap(sender:)))
self.view.addGestureRecognizer(panGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func handleTap(sender: UIPanGestureRecognizer) {
let percentThreshold: CGFloat = 0.3
let translation = sender.translation(in: view)
var horitonzalMovement: CGFloat = 0.0
var downwardMovementPercent: Float = 0.0
if self.animatorDirection.direction == .left {
horitonzalMovement = -(translation.x / view.bounds.width)
let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
downwardMovementPercent = fminf(downwardMovement, 1.0)
} else {
horitonzalMovement = +(translation.x / view.bounds.width)
let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
downwardMovementPercent = fminf(downwardMovement, 1.0)
}
let progress = CGFloat(downwardMovementPercent)
guard let interactor = interactor else { return }
switch sender.state {
case .began:
interactor.hasStarted = true
dismiss(animated: true, completion: nil)
case .changed:
interactor.shouldFinish = progress > percentThreshold
interactor.update(progress)
case .cancelled:
interactor.hasStarted = false
interactor.cancel()
case .ended:
interactor.hasStarted = false
interactor.shouldFinish ? interactor.finish() : interactor.cancel()
default: break
}
}
}
所以我可以滑动打开或关闭我的ViewController,过渡效果很好。唯一的问题是当我尝试使用UIButton关闭我的ViewController时。 NavigationController不会被解雇和冻结。有小费吗 ? :(
答案 0 :(得分:1)
我将假设你只是试图解雇ViewController而不是导航控制器本身。在这种情况下,您的IBAction应如下所示。
@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
// Technically you don't even need the self here.
// In swift self is implicit in most cases.
}
将导航控制器视为处理一堆视图。当您转到导航控制器中的新视图时,可以向堆栈添加新视图。当你解雇你弹出一个堆栈。也永远不会回头,总是解雇。如果您未能关闭堆栈上的视图控制器,则视图控制器将保留在内存中。
Here is a good tutorial on the subject.
如果需要将数据传回上一个视图,则一种技术称为展开segue。 Read up on that here.
希望这有帮助!