覆盖变量后,状态栏样式是否会发生变化?

时间:2017-04-30 04:38:39

标签: ios swift

我将以下代码添加到我的UITableViewController类中:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

但是在我重新运行应用程序后,没有任何变化,状态栏仍然是黑暗的:

输出: Error: status bar still dark style

ProjectNavigator: Project Navigator

界面生成器: Interface Builder

RestaurantTableViewController中的代码: enter image description here

我希望它是白色,我应该添加什么?

3 个答案:

答案 0 :(得分:3)

你应该致电

self.setNeedsStatusBarAppearanceUpdate()

然后状态栏样式将更新

另外,在info.plist中你应该设置

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

编辑:

我认为你的UITableViewController不是rootViewController。如果上述解决方案无效,请尝试为rootViewController设置preferredStatusBarStyleUINavigationController看起来像是基于屏幕截图的根控制器

答案 1 :(得分:1)

我宁愿在AppDelegate中这样做(application.statusBarStyle = .lightContent) - :

Error: spawn EACCES
at exports._errnoException (util.js:1026:11)
at ChildProcess.spawn (internal/child_process.js:313:11)
at exports.spawn (child_process.js:392:9)
at /Users/myusername/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:349:13
at Promise._execute (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:79:10)
at PHPCSFixer.module.exports.Beautifier.spawn (/Users/myusername/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:346:16)
at /Users/myusername/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:308:10
at tryCatcher (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:693:18)
at Promise._fulfill (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:638:18)
at PromiseArray._resolve (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise_array.js:126:19)
at PromiseArray._promiseFulfilled (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise_array.js:144:14)
at Promise._settlePromise (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:574:26)
at Promise._settlePromise0 (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:143:10)
at Async.drainQueues (/Users/myusername/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:17:14)

答案 2 :(得分:0)

如果您还想知道如何为每个子控制器设置状态栏样式:

  1. 确保在 info.plist 中设置<key>UIViewControllerBasedStatusBarAppearance</key> <true/>

  2. 确保您已经为根控制器(入口点控制器)编写了一个控制器类,我在编写问题帖子时没有创建一个控制器类,正如您在图片中看到的那样我的问题。

  3. 在根控制器类(刚刚创建)中添加以下内容:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.topViewController
    }
    

    我选择.lightContent作为我需要的内容,但您可以根据需要将其更改为.default

  4. 现在对于每个特定的视图控制器类,在我的例子中是 RestaurantTableViewController RestaurantDetailViewController ,你可以添加:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .theStyleYouLike
    }
    

    .theStyleYouLike可以是.default.lightContent