ios 11如何设置不安全区域背景颜色

问题描述 投票:0回答:5

使用 xcode 9 创建一些新的视图控制器,所以现在我有一些安全区域需要处理。

我目前正在尝试做一些全面的事情,这意味着保持不安全区域不变(因为我总是显示状态栏)并将背景颜色延伸到全屏(以保持与我以前的行为类似的行为)。

另外一点,这也会影响页面控件,因为当你有一些页面控件时,系统会将它们放在底部不安全区域,该区域也将以黑色显示。

我无法找到一种方法让背景颜色延伸到不安全区域后面。 有什么想法吗?

ios swift ios11 xcode9 safearealayoutguide
5个回答
21
投票

这看起来像是一个黑客技巧,但你可以尝试这个:
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。在这里,它通过以下方式对我有用。

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
    }

}

or

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
        return true
    }
}



这是结果:

enter image description here


10
投票

你必须应用不同的约束。您的背景颜色应该延伸到安全区域之外,一直延伸到超级视图。因此,您的约束需要设置为背景颜色的超级视图,但设置为 ui 视图的安全区域(按钮、tableView 等)


0
投票

最近我正在解决类似的问题。不同之处在于,在我看来,顶部的不安全区域必须被填充,而底部的不安全区域则不需要。另一个区别是我的背景视图实际上是在

scrollView
中,这使得约束设置更加复杂。

我尝试了上面提到的

"statusBar"
解决方案,但是我猜自iOS 13以来视图层次结构已经改变,因此这会导致崩溃。

最后我想出了这个解决方案:

首先在视图控制器视图的最顶部添加Status Bar Bg View,使其顶部、前导和尾随与安全区域的顶部、前导和尾随对齐(代码中的

statusBarBgView
)。 同时,将原始填充背景(
bgView
)的顶部约束设置为Status Bar Bg View的底部。

然后在

viewDidLoad()
中,调用以下方法:

private func fillSafeAreaIfNeeded() {
    if let _ = UIApplication.shared.keyWindow?.safeAreaInsets.top {
        statusBarBgView.translatesAutoresizingMaskIntoConstraints = false
        statusBarBgView.topAnchor.constraint(equalTo: view!.topAnchor).isActive = true // This line fills up status bar
        bgView.topAnchor.constraint(equalTo: statusBarBgView!.bottomAnchor).isActive = true
    }
    else {
        statusBarBgView.frame = CGRect.zero
    }
}

请注意,通过使用

bgView
的bottomAnchor约束
statusBarBgView
的topAnchor,可以在滚动时避免两个视图之间的间隙。


0
投票

对于那些仍在寻找这个问题的人。 我最简单的解决方案是创建一个 uiview 并约束它

    private let containerView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = //Your background color you want present
    return view
}()

然后这样约束它。设置底部安全区域(或顶部安全区域)

        containerView.topAnchor.constraint(equalTo: view.topAnchor),
        containerView.leftAnchor.constraint(equalTo: view.leftAnchor),
        containerView.rightAnchor.constraint(equalTo: view.rightAnchor),
        containerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

最后设置颜色

containerView.backgroundColor = //Color you want to set

-1
投票
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithTransparentBackground()
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = .black
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
    }
© www.soinside.com 2019 - 2024. All rights reserved.