Swift iOS -SplitViewController 不会让我隐藏 StatusBar?

Swift iOS -SplitViewController Won't Let Me Hide StatusBar?

我按照本教程顺利隐藏了状态栏smoothly hide statusBar,当我在练习项目中使用它时一切正常。我在其他没有 SplitVC 但有 tabBar 并使用 navVC & tableView 的项目中使用代码,一切正常。在那些我可以成功做到 appear/disappear.

在我的实际项目中,我为 iPad 使用了 SplitViewController。我注意到当我执行从 link 到我的 SplitViewController 的指示时,statusBar 不会隐藏。然后,我使用 Apple 的默认 MasterDetailApp 创建了一个新项目,以确保我没有做错任何事情,但它在那里也不起作用。我保留了 Apple 的所有原始代码,只添加了必要的方法来制作 statusBar appear/disappear

  1. info.plist中我添加了View controller-based status bar appearance并将其设置为YES

  2. 在情节提要中,我向 DetailVC 添加了一个紫色按钮以触发状态栏消失。我还在制作backBar按钮的方法中添加了disappear/reappear

  3. 我把statusBar disappear/disappear的所有方法都加到了DetailVC场景中。

  4. 我在场景中添加了一个tapGesture,让statusBar和backButton重新出现

我点击了 Master 场景上的加号按钮,出现了一个日期,点击它进入了 DetailVC,按下紫色 buttonPressed 隐藏了 statusBar 和 backButton,但只有 backButton 被隐藏了。我触摸背景,后退按钮重新出现。状态栏不动。

我保留了 Apple 项目的所有原始代码,并在其下方添加了我的代码:

class DetailViewController: UIViewController {

    //MARK:-  Apple's code
    @IBOutlet weak var detailDescriptionLabel: UILabel!

    func configureView() {
        if let detail = detailItem {
            if let label = detailDescriptionLabel {
                label.text = detail.description
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        configureView()

        // make backButton and statusBar reappear when scene is tapped
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(showBackButtonAndStatusBar))
        view.addGestureRecognizer(tapGesture)
    }

    var detailItem: NSDate? {
        didSet {
            configureView()
        }
    }

    //MARK:- Outside of the tapGesture in viewDidLoad everything below here is what I added

    // bool to determine wether to hide the statusBar or not
    var statusBarShouldBeHidden = false

    // api method to allow the staus bar to be hidden
    override var prefersStatusBarHidden: Bool{
        return statusBarShouldBeHidden
    }

    // api method to animate status bar appearance/disappearance
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
        return .slide
    }

    @IBAction func buttonTapped(_ sender: UIButton) {

        // 1. hide backBar button
        navigationItem.setHidesBackButton(true, animated: false)

        // 2. set bool to true
        statusBarShouldBeHidden = true

        UIView.animate(withDuration: 0.25){
            // 3. api method to allow the statusBar to disappear
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }

    //called when background is touched and added to tapGesture in viewDidLoad
    @objc func showBackButtonAndStatusBar(){

        // 1. set bool to false
        statusBarShouldBeHidden = false

        UIView.animate(withDuration: 0.25){
            // 2. bring statusBar back
            self.setNeedsStatusBarAppearanceUpdate()
        }

        // 3. bring backButton back
        navigationItem.setHidesBackButton(false, animated: true)
    }
}

如何让 SplitViewVC 让我隐藏状态栏?

您似乎正试图通过详细视图控制器隐藏状态栏。用户界面中的状态栏仅由拆分视图控制器控制,因为它位于视图控制器层次结构的顶部。因此,控制状态栏行为最简单的方法就是subclassUISplitViewController,然后重写sub[=33=中的prefersStatusBarHidden computed属性 ].此外,请确保您转到故事板并将身份检查器中的拆分视图控制器的自定义 class 字段更改为您的 subclass。

---更新答案--- @LanceSamaria 好吧,我把你上面的代码做了一些调整。首先,我只添加了按钮动作而不是点击手势。另外,我注释掉了隐藏后退按钮,因为这在 UI 中很重要,以便能够返回到主视图。无论如何,现在当你点击按钮时,SplitViewController 将隐藏状态栏。如果再次单击该按钮,状态栏将重新出现。

进口UI套件

class 详情ViewController:UIViewController {

@IBOutlet weak var detailDescriptionLabel: UILabel!

var statusBarShouldBeHidden = false

func configureView() {
    // Update the user interface for the detail item.
    if let detail = self.detailItem {
        if let label = self.detailDescriptionLabel {
            label.text = detail.description
        }
    }
}

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

/* override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
    return .slide
} */


var detailItem: NSDate? {
    didSet {
        // Update the view.
        self.configureView()
    }
}

@IBAction func buttonTapped(_ sender: UIButton) {
    // 1. hide backBar button
    //navigationItem.setHidesBackButton(true, animated: false)

    // 2. set bool to true
    statusBarShouldBeHidden = !statusBarShouldBeHidden

    UIView.animate(withDuration: 0.25){
        // 3. api method to allow the statusBar to disappear
        guard let svc = self.splitViewController as? SplitViewController else { return }
        svc.statusBarShouldBeHidden = self.statusBarShouldBeHidden
        svc.setNeedsStatusBarAppearanceUpdate()
    }
}

}

此外,还有一件非常重要的事情。下面是我的拆分视图控制器 subclass 的代码。请注意,我在拆分视图控制器和详细信息控制器中使用相同的变量名称 "statusBarShouldBeHidden"。

进口UI套件

class拆分ViewController: UI拆分ViewController {

var statusBarShouldBeHidden = false

override func viewDidLoad() {
    super.viewDidLoad()
}

override var prefersStatusBarHidden: Bool {
    return statusBarShouldBeHidden
}

}

感谢您提出这个问题。这帮助我在解决这个问题时学到了很多东西。如果您对此仍有疑问,请告诉我。