You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中.ignoresSafeArea(.keyboard)含BackgroundView时失效求修复

问题描述

点击文本框弹出键盘时视图会被上移,未添加BackgroundView()时,.ignoresSafeArea(.keyboard)可以解决该问题,但加入BackgroundView()后,该修饰符不再生效。需要实现键盘弹出时视图永远不上移的效果。

修复方案

问题根源在于BackgroundView内部的.ignoresSafeArea()会覆盖外部的键盘安全区忽略设置。只需要调整BackgroundView的安全区忽略规则,同时保留主视图的键盘安全区忽略设置即可:

  • 将BackgroundView中的.ignoresSafeArea()替换为.ignoresSafeArea(.container, edges: .all),让它仅忽略容器类的安全区(比如屏幕顶部/底部的系统栏区域),不影响键盘相关的安全区计算。
  • 保持主ZStack上的.ignoresSafeArea(.keyboard)修饰符,确保内容视图不会随键盘上移。
修改后的代码

Home View

struct Home: View {
    
    @State private var email: String = ""
    @State private var password: String = ""
    
    var body: some View {
        NavigationView {
            ZStack {
                BackgroundView()
                VStack {
                    VStack {
                        Spacer()
                        
                        VStack {
                            Text("Email or Username")
                                .font(.title)
                                .fixedSize(horizontal: false, vertical: true)
                                .minimumScaleFactor(0.7)
                                .lineLimit(1)
                                .truncationMode(.tail)
                                .frame(maxWidth: .infinity, alignment: .leading)
                                .padding(.leading)
                                .offset(y: 10)
                            
                            ZStack {
                                Color.gray
                                TextField("Email or Username", text: $email)
                                    .textContentType(.emailAddress)
                                    .keyboardType(.emailAddress)
                                    .font(.title)
                                    .fixedSize(horizontal: false, vertical: true)
                                    .minimumScaleFactor(0.7)
                                    .lineLimit(1)
                                    .truncationMode(.tail)
                                    .padding(.leading)
                            }
                            .frame(width: 300, height: 80)
                            .cornerRadius(25)
                        }
                        
                        VStack {
                            Text("Password")
                                .font(.title)
                                .fixedSize(horizontal: false, vertical: true)
                                .minimumScaleFactor(0.7)
                                .lineLimit(1)
                                .truncationMode(.tail)
                                .frame(maxWidth: .infinity, alignment: .leading)
                                .padding(.leading)
                                .offset(y: 10)
                            
                            ZStack {
                                Color.gray
                                SecureField("Password", text: $password)
                                    .font(.title)
                                    .textContentType(.password)
                                    .fixedSize(horizontal: false, vertical: true)
                                    .minimumScaleFactor(0.7)
                                    .lineLimit(1)
                                    .truncationMode(.tail)
                                    .padding(.leading)
                            }
                            .frame(width: 300, height: 80)
                            .cornerRadius(25)
                        }
                        
                        Spacer()
                        
                        VStack {
                            Button(action: {
                                print("Login Tapped")
                            }) {
                                Text("Login")
                                    .font(.title)
                                    .fixedSize(horizontal: false, vertical: true)
                                    .minimumScaleFactor(0.7)
                                    .lineLimit(1)
                                    .truncationMode(.tail)
                            }
                        }
                        .padding(.bottom, 50)
                    }
                }
            }
            .ignoresSafeArea(.keyboard)
        }
    }
}

BackgroundView

struct BackgroundView: View {
    var body: some View {
        VStack {
            ZStack {
                Circle()
                    .fill(.green)
            }
            .frame(width: UIScreen.screenWidth * 1.00, height: UIScreen.screenHeight * 1.00)
            
            Spacer()
        }
        .ignoresSafeArea(.container, edges: .all)
    }
}

内容的提问来源于stack exchange,提问作者Schwifty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 21:26:21