从隐藏状态栏的视图控制器进行交互式视图控制器转换

14
这是我的演示项目
我有两个视图控制器。主要的一个隐藏了状态栏,而第二个没有。
我创建了一个自定义驱动转换动画,从控制器一到控制器二。
当我在子视图控制器(橙色的那个)上时,我通过从顶部向底部滑动开始驱动转换。您可以看到,在拖动时状态栏会回来。并且UIButton“Hello”也在移动。
我取消了过渡。然后我再次开始它,你可以看到状态栏也回来了,但这一次,我的按钮没有移动,它停留在同一位置,就好像状态栏仍然被隐藏着一样。
有任何想法为什么至少取消一次转换后它会这样表现吗?
(我甚至没有谈论关于动画的奇怪事情,当取消时它会加倍(也许是模拟器的错误,因为它不会在我的 iPhone 6 9.1 和我的 iPhone 5 8.4 上执行)。

enter image description here


我认为这不是模拟器的问题。我在 iPhone 6(iOS 9)上测试了一下,仍然存在这个问题。 - Fomentia
我的回答解决了您的问题吗? - FredLoh
我还没有时间检查它,我会尽快告诉你。无论如何,谢谢。 - Nico
1
@Nico 如果这个回答解决了你的问题,请不要忘记将其标记为最佳答案。 - Peyman
1个回答

4

添加:import Foundation

然后添加一个outlet:

class ViewController: UIViewController { @IBOutlet weak var topConstraint: NSLayoutConstraint! ... } 当视图消失时将值更改为0,当视图出现时将其更改为20:

override func viewWillAppear(animated: Bool) {
    topConstraint.constant = 20.0
}

override func viewWillDisappear(animated: Bool) {
    topConstraint.constant = 0.0
}

完整代码(请确保记得将约束连接到出口):
import UIKit
import Foundation

class ViewController: UIViewController {

    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    let controllerTransition = InteractiveControllerTransition(gestureType: .Pan)
    let controllerTransitionDelegate = ViewController2Transition()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        controllerTransition.delegate = controllerTransitionDelegate
        controllerTransition.edge = .Bottom


    }

    override func viewWillAppear(animated: Bool) {
        topConstraint.constant = 20.0
    }

    override func viewWillDisappear(animated: Bool) {
        topConstraint.constant = 0.0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func unwindToViewController(sender: UIStoryboardSegue) { }

    override func prefersStatusBarHidden() -> Bool {
        return false
    }

    @IBAction func helloButtonAction(sender: UIButton) {
//      let storyBoard = UIStoryboard(name: "Main", bundle: nil)
//      let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
//      
//      vc.transitioningDelegate = controllerTransition
//      controllerTransition.toViewController = vc
//      
//      self.presentViewController(vc, animated: true, completion: nil)

        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
//      let nvc = storyBoard.instantiateViewControllerWithIdentifier("NavigationViewController2") as! UINavigationController
//      let vc = nvc.topViewController as! ViewController2

        let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2





//      nvc.transitioningDelegate = controllerTransition
        vc.transitioningDelegate = controllerTransition
        controllerTransition.toViewController = vc

//      self.presentViewController(nvc, animated: true, completion: nil)
        self.presentViewController(vc, animated: true, completion: nil)

    }

}

1
问题在于它更像是一个hack而不是真正的解决方案,我仍然不明白为什么它会表现出这样的行为...但它似乎可以工作:)。我只需将topConstraint.constant = 20.0更改为topConstraint.constant = statusBarHeight,并在顶部添加let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.height - Nico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接