我按照这个教程顺利隐藏了statusBar smoothly hide statusBar当我在练习项目中使用它时,一切正常。我在其他没有 SplitVC 但有 tabBar 并使用 navVC & tableView 的项目中使用代码,一切正常。在那些我可以成功地让它出现/消失。
在我的实际项目中,我使用的是适用于 iPad 的 SplitViewController。我注意到当我实现从链接到我的 SplitViewController 的指示时,statusBar 不会隐藏。然后,我使用 Apple 的默认 MasterDetailApp 创建了一个新项目,以确保我没有做错任何事情,但它在那里也不起作用。我保留了 Apple 的所有原始代码,只添加了必要的方法来使 statusBar 出现/消失
在
info.plist
中,我添加了View controller-based status bar appearance
并将其设置为YES
在 Storyboard 中,我向 DetailVC 添加了一个紫色按钮以触发状态栏消失。我还添加了使 backBar 按钮消失/重新出现的方法
我在 DetailVC 场景中添加了使 statusBar 消失/消失的所有方法。
我在场景中添加了一个 tapGesture 来让 statusBar 和 backButton 重新出现
我点击了 Master 场景上的加号按钮,出现了一个日期,点击它进入了 DetailVC,按下紫色的 buttonPressed
隐藏了 statusBar 和 backButton,但只有 backButton 被隐藏了。我触摸背景,后退按钮重新出现。 statusBar 没有移动。
我保留了 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 让我隐藏状态栏?
最佳答案
看来您正试图通过详细 View Controller 隐藏状态栏。用户界面中的状态栏仅由 Split View Controller 控制,因为它位于 View Controller 层次结构的顶部。因此,控制状态栏行为的最简单方法是子类化 UISplitViewController,然后覆盖子类中的 prefersStatusBarHidden 计算属性。另外,请确保您转到 Storyboard并将身份检查器中的 Split View Controller 的自定义类字段更改为您的子类。
---更新的答案--- @LanceSamaria 好吧,我把你上面的代码做了一些调整。首先,我只添加了按钮 Action 而不是点击手势。另外,我注释掉了隐藏后退按钮,因为这在 UI 中很重要,以便能够返回到主视图。无论如何,现在当您单击按钮时,SplitViewController 将隐藏状态栏。如果再次单击该按钮,状态栏将重新出现。
导入 UIKit
类 DetailViewController: 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()
}
}
此外,还有一件非常重要的事情。下面是我的 Split View Controller 子类的代码。请注意,我在 Split View Controller 和详细信息 Controller 中使用了相同的变量名称“statusBarShouldBeHidden”。
导入 UIKit
类 SplitViewController: UISplitViewController {
var statusBarShouldBeHidden = false
override func viewDidLoad() {
super.viewDidLoad()
}
override var prefersStatusBarHidden: Bool {
return statusBarShouldBeHidden
}
感谢您提出这个问题。这帮助我在解决这个问题时学到了很多东西。如果您对此仍有疑问,请告诉我。
关于ios - Swift iOS -SplitViewController 不会让我隐藏 StatusBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267883/