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

SwiftUI:safeAreaInset(.bottom)下Max按钮键盘显示时点击失效问题

SwiftUI键盘弹出时Max按钮无法点击的解决方案

问题根源:你给ScrollView添加.safeAreaInset(edge: .bottom)后,ScrollView的内容布局范围会被调整,导致Max按钮所在区域虽然视觉可见,但超出了ScrollView的可交互范围,因此无法响应点击。

方案一:使用Toolbar实现键盘悬浮按钮(推荐,iOS 16+原生支持)

直接利用SwiftUI的Toolbar将Submit按钮绑定到键盘上方,完全不影响原有视图的交互逻辑:

import SwiftUI

struct ContentView: View {
    @State private var amountText: String = ""
    @State private var balance: String = "100.00"
    
    var body: some View {
        ScrollView(showsIndicators: false) {
            VStack(alignment: .leading, spacing: 20) {
                Spacer()
                    .frame(height: 500)
                
                Text("Enter Amount")
                    .font(.headline)
                
                TextField("Enter amount", text: $amountText)
                    .padding()
                    .background(Color.gray.opacity(0.2))
                    .cornerRadius(8)
                    .keyboardType(.decimalPad)
                
                HStack {
                    Spacer()
                    Text("Balance: \(balance)")
                        .font(.subheadline)
                        .foregroundColor(.gray)
                    
                    Button("Max") {
                        amountText = balance
                    }
                    .font(.subheadline)
                    .foregroundColor(.blue)
                }
            }
            .padding(.horizontal)
        }
        .toolbar {
            ToolbarItem(placement: .keyboard) {
                Button("Submit") {
                    print("Submitted with amount: \(amountText)")
                }
                .padding()
                .frame(maxWidth: .infinity)
                .background(Color.blue)
                .foregroundColor(.white)
                .cornerRadius(8)
            }
        }
    }
}

方案二:调整safeAreaInset的附加对象

如果坚持使用safeAreaInset,将其附加到外层容器而非ScrollView,避免压缩ScrollView的交互范围:

import SwiftUI

struct ContentView: View {
    @State private var amountText: String = ""
    @State private var balance: String = "100.00"
    
    var body: some View {
        ZStack {
            ScrollView(showsIndicators: false) {
                VStack(alignment: .leading, spacing: 20) {
                    Spacer()
                        .frame(height: 500)
                    
                    Text("Enter Amount")
                        .font(.headline)
                    
                    TextField("Enter amount", text: $amountText)
                        .padding()
                        .background(Color.gray.opacity(0.2))
                        .cornerRadius(8)
                        .keyboardType(.decimalPad)
                    
                    HStack {
                        Spacer()
                        Text("Balance: \(balance)")
                            .font(.subheadline)
                            .foregroundColor(.gray)
                        
                        Button("Max") {
                            amountText = balance
                        }
                        .font(.subheadline)
                        .foregroundColor(.blue)
                    }
                }
                .padding(.horizontal)
            }
        }
        .safeAreaInset(edge: .bottom) {
            Button("Submit") {
                print("Submitted with amount: \(amountText)")
            }
            .padding()
            .frame(maxWidth: .infinity)
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
            .padding(.horizontal)
            .padding(.bottom, 8) // 手动调整与键盘的间距
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 16:55:05