2016-01-31 189 views
0

我用它来获取状态栏的框架尝试:如何更改状态栏的框架?

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) 
+0

我需要更改状态栏的框架和[这](https://github.com/ndesai/InstagramStatusBarTest/blob/master/InstagramStatusBarTest/ViewController.m)是我唯一的出路在网上找到,它给了我一个不变的对象。我在哪里可以访问可变状态栏框? – Jay

+0

框架矩形定义状态栏的区域。 (只读)https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instp/UIApplication/statusBarFrame –

+0

@LeoDabus,这意味着状态酒吧帧不能在迅速操纵,但他们可以在Obj-c? – Jay

回答

2

我我会根据你写的关于你对这个问题的意图的评论给你一个答案。


解决方案

你可以得到的状态栏由UIViewController覆盖方法prefersStatusBarHidden侧边栏打开侧边栏(类似于松弛的应用程序)时消失。它应该是这样的:

override func prefersStatusBarHidden() -> Bool { 
    return true 
} 

您还可以修改这个外观与两种方法:preferredStatusBarStylepreferredStatusBarUpdateAnimation


我做了一个简单的项目来说明这一点。有可能以许多不同的方式实现一个侧边栏,所以我将这个例子基于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 
    } 
} 

Storyboard of the example project, highlighting the segue attached to the <code>UIButton</code>


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 
    } 

} 
+0

谢谢,但是,我不认为我可以在我的项目中实现这一点,因为我用UIView来持有侧栏。我使用UIViewAnimation来移动与当前VC相关的视图,并将边栏移动了我设置原始视图的量。所以从本质上讲,我不会冒犯任何其他VC。因此,如果使用一个VC,如何刷新视图以重新触发prefersstatusbarhidden函数? – Jay

+0

我已经更新了我的回答 – Kumuluzz

+0

谢谢@Kumuluzz!这解决了这个问题。尽管我有一个问题:为了创建侧边栏,我划分了UIView子类,所以它仍然可以重用,并不是因为它是最好的,但它是我能想出的唯一解决方案。通过创建一个UIVC,我可以获得什么优势?我绝对想成为一名更好的程序员,所以如果你能尽可能详细地阐述它,我会非常感激! – Jay