ios - 如何在 Swift 中使 iPhone 状态栏透明?

标签 ios swift statusbar

我希望隐藏导航栏和状态栏以使其透明。我在下面有一张图片,我想占据整个空间。到目前为止,我已经隐藏了导航栏,但状态栏仍然是白色的——这是在 viewDidLoad() 中:

self.navigationController?.setNavigationBarHidden(true, animated: true)

另外,如何将返回按钮重新添加到导航栏?

谢谢!

我当前的 iPhone 模拟器的屏幕截图:

enter image description here

Screen shot after add Mohy Gh's code

根据 Mohy Gh 的代码编辑:

最佳答案

Swift 3:对于整个应用:

在您的 AppDelegatedidFinishLaunchingWithOptions 方法中执行此操作,而不是隐藏导航栏:

// Sets navigationBar's background to a blank/empty image       
UINavigationBar.appearance().setBackgroundImage(UIImage(),
                                                for: .default)

// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = true

编辑:要在您的 View Controller 中隐藏一个特定 View Controller 的navigationBar,请这样做:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let navigationBar = self.navigationController?.navigationBar

    navigationBar?.setBackgroundImage(UIImage(), for: .default)
    navigationBar?.shadowImage = UIImage()
    navigationBar?.isTranslucent = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    let navigationBar = self.navigationController?.navigationBar

    navigationBar?.shadowImage = nil
    navigationBar?.setBackgroundImage(nil, for: .default)
    navigationBar?.isTranslucent = false
}

在使 navigationBar 透明后,您需要将 imageView 的顶部约束设置为 topLayoutGuid = 0

如果你还想为一个 viewController 隐藏 statusBar,也把它放在你想要的 viewController 中:

override var prefersStatusBarHidden: Bool {
    return true
}

关于ios - 如何在 Swift 中使 iPhone 状态栏透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45395581/

相关文章:

iphone - 在 xcode 中保存/共享字符串

objective-c - 换肤 iOS 应用程序

ios - Storyboard和模拟器之间的 XCode 对齐/间距?

android - 加载页面( View )时如何在ionic中播放音频mp3

ios - 带有自定义单元格的 TableViewController swift

ios - 如何正确设置 MKPointAnnotation 坐标变化的动画

iphone - 将 UIView 或 UIWindow 放置在 Statusbar 上方

ios - 状态栏背景颜色不会更改为自定义颜色

regex - 检索两个特殊字符之间的字符串

javascript - 如何在弹出窗口中启用window.status?