我我会根据你写的关于你对这个问题的意图的评论给你一个答案。
解决方案
你可以得到的状态栏由UIViewController
覆盖方法prefersStatusBarHidden
侧边栏打开侧边栏(类似于松弛的应用程序)时消失。它应该是这样的:
override func prefersStatusBarHidden() -> Bool {
return true
}
您还可以修改这个外观与两种方法:preferredStatusBarStyle
和preferredStatusBarUpdateAnimation
例
我做了一个简单的项目来说明这一点。有可能以许多不同的方式实现一个侧边栏,所以我将这个例子基于popover。您的实施将取决于您如何实施侧边栏。
我做了一个简单的故事板,每个都有两个UIViewController
和一个UIButton
。当点击第一个UIButton
时,它将触发一个类型为Present As Popover
的继续,它将显示第二个控制器。
第一个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
}
}
UPDATE 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
}
}
我需要更改状态栏的框架和[这](https://github.com/ndesai/InstagramStatusBarTest/blob/master/InstagramStatusBarTest/ViewController.m)是我唯一的出路在网上找到,它给了我一个不变的对象。我在哪里可以访问可变状态栏框? – Jay
框架矩形定义状态栏的区域。 (只读)https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instp/UIApplication/statusBarFrame –
@LeoDabus,这意味着状态酒吧帧不能在迅速操纵,但他们可以在Obj-c? – Jay