优化键盘收起:支持点击标题栏和空白区域

This commit is contained in:
2026-07-12 16:07:14 +08:00
parent bfc43964d8
commit aa45062bd7
+40 -3
View File
@@ -15,6 +15,11 @@ struct FormatterView: View {
var body: some View { var body: some View {
NavigationStack { NavigationStack {
VStack(spacing: 0) {
headerView
.background(Color(hex: "f2f2f7"))
ScrollView {
VStack(spacing: 16) { VStack(spacing: 16) {
TextEditor(text: $inputText) TextEditor(text: $inputText)
.frame(minHeight: 160) .frame(minHeight: 160)
@@ -25,6 +30,14 @@ struct FormatterView: View {
RoundedRectangle(cornerRadius: 8) RoundedRectangle(cornerRadius: 8)
.stroke(Color(.systemGray4), lineWidth: 1) .stroke(Color(.systemGray4), lineWidth: 1)
) )
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("完成") {
dismissKeyboard()
}
}
}
Button(action: formatText) { Button(action: formatText) {
Label("格式化", systemImage: "wand.and.stars") Label("格式化", systemImage: "wand.and.stars")
@@ -60,13 +73,15 @@ struct FormatterView: View {
} }
} }
Spacer() Spacer(minLength: 16)
} }
.padding() .padding()
.navigationTitle("文案格式化") }
.background(Color(hex: "f2f2f7"))
}
.background(Color(hex: "f2f2f7").ignoresSafeArea()) .background(Color(hex: "f2f2f7").ignoresSafeArea())
.onTapGesture { .onTapGesture {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) dismissKeyboard()
} }
.alert("提示", isPresented: $showEmptyAlert) { .alert("提示", isPresented: $showEmptyAlert) {
Button("确定", role: .cancel) {} Button("确定", role: .cancel) {}
@@ -89,9 +104,31 @@ struct FormatterView: View {
.transition(.opacity) .transition(.opacity)
} }
} }
.navigationBarHidden(true)
} }
} }
private var headerView: some View {
HStack {
Text("文案格式化")
.font(.largeTitle.bold())
.foregroundStyle(.primary)
Spacer()
}
.frame(maxWidth: .infinity, minHeight: 44)
.padding(.horizontal)
.padding(.top, 8)
.padding(.bottom, 8)
.contentShape(Rectangle())
.onTapGesture {
dismissKeyboard()
}
}
private func dismissKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
private func formatText() { private func formatText() {
if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
showEmptyAlert = true showEmptyAlert = true