简体   繁体   English

如何在 swift 4.2 中设置安全区域背景颜色?

[英]How to set safe area background color in swift 4.2?

I'm a very beginner in swift我是 swift 的初学者

I have problem with the safe area view.我对安全区域视图有疑问。

safe area view安全区域视图

For the top(some said "Status bar") I've done with this code for changing the background color对于顶部(有人说“状态栏”),我已经完成了更改背景颜色的代码

if #available(iOS 13.0, *) {
            let app = UIApplication.shared
            let statusBarHeight: CGFloat = app.statusBarFrame.size.height

            let statusbarView = UIView()
            statusbarView.backgroundColor = hexStringToUIColor(hex: "#7f0000")
            view.addSubview(statusbarView)

            statusbarView.translatesAutoresizingMaskIntoConstraints = false
            statusbarView.heightAnchor
                .constraint(equalToConstant: statusBarHeight).isActive = true
            statusbarView.widthAnchor
                .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
            statusbarView.topAnchor
                .constraint(equalTo: view.topAnchor).isActive = true
            statusbarView.centerXAnchor
                .constraint(equalTo: view.centerXAnchor).isActive = true

        } else {
            let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = hexStringToUIColor(hex: "#7f0000")
        }

I have no idea how to change the bottom safe area view background color?我不知道如何更改底部安全区域视图背景颜色?

my Main.storyboard my storyboard我的 Main.storyboard我的 storyboard

just set the color to your view: view.backgroundColor =.blue只需将颜色设置为您的视图:view.backgroundColor =.blue

and set constraints to your view, that it is as big as the safe area.并为您的视图设置约束,使其与安全区域一样大。

I was struggling with this one for an hour so I thought I'd share my solution if anyone wants it.我为此苦苦挣扎了一个小时,所以我想如果有人想要我会分享我的解决方案。

This DID NOT work for me这对我不起作用

extension UIApplication
{
    var statusBarView: UIView?
    {
        return value(forKey: "statusBar") as? UIView
    }
}
// Calling this 👇 crashed
// UIApplication.shared.statusBarView?.backgroundColor = UIColor.black

So I just made a different extension所以我只是做了一个不同的扩展

extension UIViewController
{
  func topColouredBlack()
  {
     let colouredTopBlack = UIView()
     view.addSubview(colouredTopBlack)
     colouredTopBlack.translatesAutoresizingMaskIntoConstraints = false
     colouredTopBlack.backgroundColor = .black

     NSLayoutConstraint.activate([
        colouredTopBlack.topAnchor.constraint(equalTo: view.topAnchor),
        colouredTopBlack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        colouredTopBlack.widthAnchor.constraint(equalTo: view.widthAnchor),
    ])
  }
}

I just call topColouredBlack() wherever I want & I made another one of a different colour when I want to change it back.我只是在任何我想要的地方调用 topColouredBlack() 并且当我想将它改回来时,我制作了另一种不同颜色的颜色。

func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
{
    topColouredBlack()
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
  // topColouredWhite()
}

have you tried this code?你试过这个代码吗?

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
  }
}

Though my solution here is solved on post iOS 13, I think this may help:尽管我的解决方案已在 iOS 13 后解决,但我认为这可能会有所帮助:

https://stackoverflow.com/a/66148059/4963585 https://stackoverflow.com/a/66148059/4963585

Basically the solution is make a view with the same background color that constrains to safe area's top, then activate that additional view's topAnchor.基本上,解决方案是创建一个具有相同背景颜色的视图,以限制安全区域的顶部,然后激活该附加视图的 topAnchor。 If your view requires scrolling, you can also set the anchor between the original background and that added view to prevent scrolling gap.如果您的视图需要滚动,您还可以在原始背景和添加的视图之间设置锚点,以防止滚动间隙。

Just set color of safeAreaLayoutGuide for whole app in SceneDelegate:只需在 SceneDelegate 中为整个应用设置 safeAreaLayoutGuide 的颜色:

  window?.safeAreaLayoutGuide.owningView?.backgroundColor = .green

let app = UIApplication.shared let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: app.statusBarFrame.height)) statusBarView.backgroundColor = UIColor.red strong text self.view.addSubview(statusBarView) let app = UIApplication.shared let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: app.statusBarFrame.height)) statusBarView.backgroundColor = UIColor.red强文本self.view.addSubview (状态栏视图)

you can use this code it work for me你可以使用这个代码它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在 iOS 11 中为顶部安全区域设置背景颜色 - Set background color for top safe area only in iOS 11 如何在横向的安全区域中设置 SwiftUI 列表标题的背景颜色? - How can I set background color of SwiftUI List Header in safe area in Landscape? 如何在UIScrollView中为内容区域设置背景颜色? - How to set background color to the content area in UIScrollView? 如何快速为新 iPhone 设备设置安全区域布局 - How to set Safe Area Layout for new IPhone Devices in swift SwiftUI 来自文本的背景颜色忽略安全区域,即使它未设置为忽略 - SwiftUI background color from Text ignores safe area even though it is NOT set to ignore 如何在 swift 编程中在 UIimage 中设置背景颜色 - how to set a background color in UIimage in swift programming 如何为ios 11设置不安全区域背景颜色 - How to set unsafe area background color for ios 11 如何使 SwiftUI 列表行背景颜色扩展到整个宽度,包括安全区域之外 - How can I make a SwiftUI List row background color extend the full width, including outside of the safe area 更改导航栏安全区域的颜色 Swift UIKit - Changing Color of the Navigation Bar's Safe Area Swift UIKit Swift:iPhone上的安全区域 - Swift: safe area on iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM