简体   繁体   English

如何在 SwiftUI 中使用 edgesIgnoringSafeArea,但让子视图尊重安全区域?

[英]How can I use edgesIgnoringSafeArea in SwiftUI, but make a child view respect the safe area?

I'm building a UI is SwiftUI with a list view, and I want to put a panel over part of the list view with a button on it.我正在构建一个带有列表视图的 UI 是 SwiftUI,我想在列表视图的一部分上放置一个面板,上面有一个按钮。

On devices with a safe area at the bottom (that don't have a physical home button) I want the panel to go to the bottom of the screen.在底部有安全区域(没有物理主页按钮)的设备上,我希望面板位于屏幕底部的 go。 But I want to make sure that the button on the panel doesn't extend into the safe area.但我想确保面板上的按钮不会延伸到安全区域。

In my example code, the HStack is the panel that contains the button.在我的示例代码中, HStack是包含按钮的面板。 I tried adding .edgesIgnoringSafeArea(.bottom) to it, but that doesn't allow it to extend to the bottom.我尝试向其中添加.edgesIgnoringSafeArea(.bottom) ,但这不允许它延伸到底部。 This is kind of confusing to me, because the List that's in the same ZStack extends into the bottom safe area.这让我有点困惑,因为同一个ZStack中的列表延伸到底部安全区域。

When I add .edgesIgnoringSafeArea(.bottom) to the ZStack , the HStack (blue panel) is allowed to extend into the safe area at the bottom of the screen.当我将.edgesIgnoringSafeArea(.bottom)添加到ZStack时,允许HStack (蓝色面板)延伸到屏幕底部的安全区域。 This is what I want, but then once I do that, how can I tell the Button that's a child of the HStack to not ignore the safe area?这就是我想要的,但是一旦我这样做了,我怎么能告诉作为HStack子项的Button不要忽略安全区域?

I know how I would do this in UIKit, but I'm really hoping to be able to build this UI in SwiftUI. I could manually add some padding or Spacers to add extra padding under the Button to account for the safe area, but then there would be extra spacing on devices with a home button that don't have a bottom safe area.我知道如何在 UIKit 中执行此操作,但我真的希望能够在 SwiftUI 中构建此 UI。我可以手动添加一些填充或 Spacers 以在Button下添加额外的填充以说明安全区域,但随后带有主页按钮但没有底部安全区域的设备会有额外的间距。 I'd like to figure out an elegant solution where I can rely on the system to define its safe areas instead of manually defining any spacing numerically or creating conditional situations for different devices.我想找出一个优雅的解决方案,我可以依靠系统来定义其安全区域,而不是手动定义任何数字间距或为不同设备创建条件情况。

struct ContentView: View {
    var body: some View {

        ZStack(alignment: .bottom) {

                    List {
                        Text("Item 1")
                        Text("Item 2")
                        Text("Item 3")
                    }

                    HStack {
                        Spacer()
                        Button(action: {
                            // do something
                        }){
                            Text("Button")
                                .font(.title)
                                .padding()
                                .background(Color.green)
                                .foregroundColor(.white)
                                .cornerRadius(15)
                        }.padding()
                        Spacer()
                    }.background(Color.blue).edgesIgnoringSafeArea(.bottom)
                }
    }
}

我想要的失败尝试的屏幕截图

Whatever view you draw in the background modifier will use the frame of the view it's modifying.您在背景修改器中绘制的任何视图都将使用它正在修改的视图的框架。 So you need to tell THAT view to ignore the safe area.所以你需要告诉那个视图忽略安全区域。

.background(Color.blue.edgesIgnoringSafeArea(.bottom))

You can access the height of the safe area using a GeometryReader.您可以使用 GeometryReader 访问安全区域的高度。 Then you could use the height of the safe area as bottom padding or offset on your button.然后您可以使用安全区域的高度作为按钮的底部填充或偏移。
https://developer.apple.com/documentation/swiftui/geometryproxy https://developer.apple.com/documentation/swiftui/geometryproxy

struct ContentView: View {
    var body: some View {

        GeometryReader { proxy in
            ZStack(alignment: .bottom) {

                List {
                    Text("Item 1")
                    Text("Item 2")
                    Text("Item 3")
                }

                HStack {
                    Spacer()
                    Button(action: {
                        // do something
                    }){
                        Text("Button")
                            .font(.title)
                            .padding()
                            .background(Color.green)
                            .foregroundColor(.white)
                            .cornerRadius(15)
                    }
                    .padding(.bottom, proxy.safeAreaInsets.top)
                    Spacer()
                }.background(Color.blue)
            }.edgesIgnoringSafeArea(.bottom)
        }
    }
}

Adding on to @Joe Susnick's answer, the full code is:添加到@Joe Susnick 的回答中,完整的代码是:

VStack {

}.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(red: 0.357, green: 0.784, blue: 0.016, opacity: 0.5).ignoresSafeArea()
)

Anything inside of VStack respects the safe area whereas the background fills the whole screen. VStack 内部的任何内容都尊重安全区域,而背景会填满整个屏幕。

edgesIgnoringSafeArea(_:) is Deprecated edgeIgnoringSafeArea(_:) 已弃用

use the following:使用以下内容:


.background(Color.blue.ignoresSafeArea(edges: .bottom))

暂无
暂无

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

相关问题 如何使 SwiftUI 列表行背景颜色扩展到整个宽度,包括安全区域之外 - How can I make a SwiftUI List row background color extend the full width, including outside of the safe area UIStoryboard约束问题与iPhoneX中的安全区域有关(如何创建具有相等高度的安全区域的View?) - UIStoryboard constraint issue with safe area in iPhoneX (How can I create View with equal height of safe area?) 如何在横向的安全区域中设置 SwiftUI 列表标题的背景颜色? - How can I set background color of SwiftUI List Header in safe area in Landscape? SwiftUI 视图在安全区域附近自行调整 - SwiftUI view is adjusting itself near the safe area 我应该如何使用 Firestore 使用 SwiftUI 制作动态视图 - How should I use Firestore to make a dynamic view with SwiftUI 如何在 Windows 上使用 SwiftUI 制作 iOS 应用程序? - How can I use SwiftUI on Windows to make an iOS app? 如何在swiftUI中删除顶部安全区域 - How to remove the top safe area in swiftUI 如何在 SwiftUI 中访问安全区域大小? - How to access safe area size in SwiftUI? 在iPhone X的AR SceneKit View中添加约束时,如何忽略安全区域? - How Can I Ignore Safe Area When Adding Constraints to AR SceneKit View on iPhone X? SwiftUI:在主视图上显示模态(实际上不是模态)视图时,如何限制视图的可点击区域? - SwiftUI: How can I restrict the tappable area of a view when presenting a modal(actually not modal) view over a main view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM