简体   繁体   中英

How to remove the top safe area in swiftUI

我正在使用 SwiftUI 开发屏幕,想在屏幕的顶部安全区域显示我的视图,有什么方法可以在 SwiftUI 中实现这一点?

Use this modifier:

.edgesIgnoringSafeArea(.top)

If you want to ignore all the safe area insets you can pass .all

SwiftUI 2.0

You can use this modifier to pass in the region and the edges you need to ignore

.ignoresSafeArea(.container, edges: .top)

Note: Both parameters are optional

This modifier is very useful when you need to react to the keyboard as the region. The old modifier is not deprecated yet and you can still use it as the primary method:


SwiftUI 1.0

Use this modifier if need to support iOS 13

.edgesIgnoringSafeArea(.top)

Note:

You can pass .all for both modifiers if you wish to ignore all edges.

By default SwiftUI views will mostly stay inside the safe area. It will go to the bottom of the screen, but it won't go near any notch at the top of the device. If you want your view to be truly full screen, then you should use the edgesIgnoringSafeArea() modifier.

struct MyView : View {

    var body: some View {
         Text("Welcome to Swift UI")
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .edgesIgnoringSafeArea(.top)
     }
    }
/**
 This extension is needed because of deprecating edgesignoringsafearea for iOS 13.0–15.2
 https://developer.apple.com/documentation/swiftui/menu/edgesignoringsafearea(_:)
 */
public extension View {
    @ViewBuilder
    func expandViewOutOfSafeArea(_ edges: Edge.Set = .all) -> some View {
        if #available(iOS 14, *) {
            self.ignoresSafeArea(edges: edges)
        } else {
            self.edgesIgnoringSafeArea(edges) // deprecated for iOS 13.0–15.2, look upper
        }
    }
}

How to use:

MyView()
   .expandViewOutOfSafeArea()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM