如何更改状态栏的框架?
How can I change the frame of the status bar?
我试过用它来获取状态栏的框架:
var statusBarWindow = UIApplication.sharedApplication().valueForKey("statusBarWindow")
然而,当我尝试更改框架时,它说该值是不可变的:
statusBarWindow?.frame = CGRect(x: -oldViewFrame, y: statusBarWindow.frame.origin.y, width: statusBarWindow.frame.size.width, height: statusBarWindow.frame.size.height)
我会根据你写的关于这个问题的意图的评论给你答案。
解决方案
您可以通过为侧边栏覆盖 UIViewController
中的方法 prefersStatusBarHidden
来让状态栏在打开侧边栏时消失(类似于 Slack 应用程序)。它应该类似于以下内容:
override func prefersStatusBarHidden() -> Bool {
return true
}
您还可以使用以下两种方法修改它的外观:preferredStatusBarStyle
和 preferredStatusBarUpdateAnimation
例子
我做了一个简单的项目来说明这一点。可以通过许多不同的方式实现侧边栏,因此我将此示例基于弹出窗口。您的实施将取决于您如何实施边栏。
我制作了一个简单的故事板,每个故事板有两个 UIViewController
和一个 UIButton
。当单击第一个 UIButton
时,它将触发类型为 Present As Popover
的 segue,这将显示第二个控制器。
第一个UIViewController
没有代码(一切都在故事板中完成),但第二个UIViewController
有隐藏状态栏的代码。
我在下面附上了故事板的屏幕截图和第二个 UIViewController
的代码。
//
// PopController.swift
// SidebarHideStatus
//
// Created by Stefan Veis Pennerup on 31/01/16.
// Copyright © 2016 Kumuluzz. All rights reserved.
//
import UIKit
class PopController: UIViewController {
// MARK: - Storyboard actions
@IBAction func backButtonPressed(sender: UIButton) {
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - Status bar
override func prefersStatusBarHidden() -> Bool {
return true
}
}
更新 1:OP 使用 UIView
而不是边栏的 UIViewController
。
首先,我建议您将侧边栏分解为一个单独的 UIViewController
,因为这将使它在未来更加可重用,但这是一个完全不同的讨论,可能会持续数天!
为了隐藏状态栏,那还是要用到我之前强调的回调方法,不过你只需要调用方法setNeedsStatusBarAppearanceUpdate
来手动更新即可。
我已经使用以下代码更新了最初的 UIViewController
并删除了 segue 以演示此方法。
//
// ViewController.swift
// SidebarHideStatus
//
// Created by Stefan Veis Pennerup on 31/01/16.
// Copyright © 2016 Kumuluzz. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// MARK: - Properties
private var isSidebarShown = false
// MARK: - Storyboard outlets
@IBAction func buttonPressed(sender: UIButton) {
isSidebarShown = !isSidebarShown
setNeedsStatusBarAppearanceUpdate()
}
// MARK: - Status bar
override func prefersStatusBarHidden() -> Bool {
return isSidebarShown
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
// NOTE: This method has no effect now when
// using the method setNeedsStatusBarAppearanceUpdate()
return .Slide
}
}
我试过用它来获取状态栏的框架:
var statusBarWindow = UIApplication.sharedApplication().valueForKey("statusBarWindow")
然而,当我尝试更改框架时,它说该值是不可变的:
statusBarWindow?.frame = CGRect(x: -oldViewFrame, y: statusBarWindow.frame.origin.y, width: statusBarWindow.frame.size.width, height: statusBarWindow.frame.size.height)
我会根据你写的关于这个问题的意图的评论给你答案。
解决方案
您可以通过为侧边栏覆盖 UIViewController
中的方法 prefersStatusBarHidden
来让状态栏在打开侧边栏时消失(类似于 Slack 应用程序)。它应该类似于以下内容:
override func prefersStatusBarHidden() -> Bool {
return true
}
您还可以使用以下两种方法修改它的外观:preferredStatusBarStyle
和 preferredStatusBarUpdateAnimation
例子
我做了一个简单的项目来说明这一点。可以通过许多不同的方式实现侧边栏,因此我将此示例基于弹出窗口。您的实施将取决于您如何实施边栏。
我制作了一个简单的故事板,每个故事板有两个 UIViewController
和一个 UIButton
。当单击第一个 UIButton
时,它将触发类型为 Present As Popover
的 segue,这将显示第二个控制器。
第一个UIViewController
没有代码(一切都在故事板中完成),但第二个UIViewController
有隐藏状态栏的代码。
我在下面附上了故事板的屏幕截图和第二个 UIViewController
的代码。
//
// PopController.swift
// SidebarHideStatus
//
// Created by Stefan Veis Pennerup on 31/01/16.
// Copyright © 2016 Kumuluzz. All rights reserved.
//
import UIKit
class PopController: UIViewController {
// MARK: - Storyboard actions
@IBAction func backButtonPressed(sender: UIButton) {
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - Status bar
override func prefersStatusBarHidden() -> Bool {
return true
}
}
更新 1:OP 使用 UIView
而不是边栏的 UIViewController
。
首先,我建议您将侧边栏分解为一个单独的 UIViewController
,因为这将使它在未来更加可重用,但这是一个完全不同的讨论,可能会持续数天!
为了隐藏状态栏,那还是要用到我之前强调的回调方法,不过你只需要调用方法setNeedsStatusBarAppearanceUpdate
来手动更新即可。
我已经使用以下代码更新了最初的 UIViewController
并删除了 segue 以演示此方法。
//
// ViewController.swift
// SidebarHideStatus
//
// Created by Stefan Veis Pennerup on 31/01/16.
// Copyright © 2016 Kumuluzz. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// MARK: - Properties
private var isSidebarShown = false
// MARK: - Storyboard outlets
@IBAction func buttonPressed(sender: UIButton) {
isSidebarShown = !isSidebarShown
setNeedsStatusBarAppearanceUpdate()
}
// MARK: - Status bar
override func prefersStatusBarHidden() -> Bool {
return isSidebarShown
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
// NOTE: This method has no effect now when
// using the method setNeedsStatusBarAppearanceUpdate()
return .Slide
}
}