[英]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
。 当单击第一个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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.