SwiftUI:如何将我的视图推到上方以使用文本字段?(halfASheet,TextEditor)
SwiftUI : How I can push my view above to use textfield?(halfASheet, TextEditor)
import SwiftUI
import HalfASheet
struct FolderListView: View {
@EnvironmentObject var vm : FolderListViewModel
@Environment(\.presentationMode) var presentationMode
@State private var folderName : String = ""
@State private var showAddSheet : Bool = false
@State private var showAlert : Bool = false
var body: some View {
ZStack{
NavigationView{
ZStack{
if vm.folders.count == 0 {
NoFolderView()
} else {
List {
ForEach(vm.folders) { folder in
NavigationLink(destination: {
MemoListView(folder: folder)
}, label: {
FolderRowView(folder: folder)
})
}
.onDelete(perform: vm.deleteFolder)
}//list
.listStyle(.plain)
.listRowBackground(Color.clear)
}
}
.navigationTitle("Folders ")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.showAddSheet.toggle()
}, label: {
Image(systemName: "plus")
})
}
}
}
HalfASheet(isPresented: $showAddSheet) {
halfSheetView
}
.height(.proportional(0.25))
.contentInsets(EdgeInsets(top: 20, leading: 10, bottom: 10, trailing: 10))
}
.ignoresSafeArea()
}
private var halfSheetView : some View {
VStack(alignment: .leading) {
Text("폴더명을 입력하세요 ")
.font(.headline.bold())
TextField("Insert folder's name", text: $folderName)
.autocapitalization(.none)
.disableAutocorrection(true)
Divider()
.padding(.bottom, 10)
Button(action: {
if vm.checkFolderNameCount(folderName: folderName) {
vm.addNewFolder(folderName: folderName)
self.folderName = ""
self.showAddSheet.toggle()
} else {
self.showAlert.toggle()
}
}, label: {
Text("Save".uppercased())
.font(.headline)
.foregroundColor(.white)
.frame(maxWidth : .infinity)
.frame(height : 50)
.background(Color.accentColor)
.cornerRadius(10)
})
.alert(isPresented : $showAlert) {
Alert(title: Text("Naming Error "), message: Text("Letters for folder's name is too short. Please check it one more time."), dismissButton: .cancel())
}
Spacer()
}//vst
.ignoresSafeArea(.keyboard, edges: .bottom)
.padding()
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
FolderListView()
}
}
此代码首先是关于我的 'halfASheet'(库)代码。
导入 SwiftUI
构造 NewMemoView:视图 {
let folder : FolderModel
let memo : MemoModel?
@EnvironmentObject var vm : FolderListViewModel
@Environment(\.presentationMode) var presentationMode
@State private var title : String = ""
@State private var content : String = ""
@State private var color : Color = .yellow.opacity(0.3)
@State private var showAlert : Bool = false
@State private var pickedImportance : String = "Easy "
@State private var isEditMode : Bool = false
let importances = ["Easy ", "Normal ☀️", "Important "]
init(memo : MemoModel?, folder : FolderModel) {
self.folder = folder
self.memo = memo
if let memo = memo {
_title = State(initialValue: memo.title)
_content = State(initialValue: memo.content)
_color = State(initialValue: memo.color)
_pickedImportance = State(initialValue: memo.isImportant)
_isEditMode = State(initialValue: true)
}
}
var body: some View {
Form {
Section(header : Text("Title ")) {
TextField("Input the title", text: $title)
.autocapitalization(.none)
.disableAutocorrection(true)
}
Section(header : Text("Importance ✅")) {
Picker("", selection: $pickedImportance) {
ForEach(importances, id: \.self) {
Text([=11=])
}
}
.pickerStyle(.segmented)
}
Section(header : Text("Content ✏️")) {
TextEditor(text: $content)
.autocapitalization(.none)
.disableAutocorrection(true)
.frame(maxWidth : .infinity)
.frame(height : UIScreen.main.bounds.height * 0.3)
}
Section(header : Text("Select Color")) {
ColorPicker("Memo Color", selection: $color, supportsOpacity: false)
}
Button(action: {
if isEditMode {
if vm.checkMemoStatus(title: title, content: content) {
vm.editMemo(folder: folder, memo: memo!, title: title, content: content, color: color, isImportant: pickedImportance)
presentationMode.wrappedValue.dismiss()
} else {
self.showAlert.toggle()
}
} else {
if vm.checkMemoStatus(title: title, content: content) {
vm.addMemo(folder: folder, title: title, content: content, color: color, isImportant: pickedImportance)
presentationMode.wrappedValue.dismiss()
} else {
self.showAlert.toggle()
}
}
}, label: {
Text(isEditMode ? "Edit".uppercased() : "save".uppercased())
.fontWeight(.bold)
})
.alert(isPresented : $showAlert) {
Alert(title: Text("Warning "), message: Text("Check your title and content, please."), dismissButton: .cancel())
}
}//form
.navigationTitle("Add Memo ")
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
接下来的代码是关于带有文本字段和文本编辑器的表单。
(这个截图是关于halfSheet的)
我想做的是,当我点击半张纸的文本字段时,
我想把我的观点推上去。
我试过使用滚动视图,但这不起作用。
我该如何处理这种情况?
谢谢!
您可以在关注文本字段时更改高度值
@State var isEditing = false
TextField("", text: $text, onEditingChanged: { edit in
self.isEditing = edit
})
HalfASheet(isPresented: $showAddSheet) {
halfSheetView
}
.height(.proportional(isEditing ? 0.25 : 0.75))
import SwiftUI
import HalfASheet
struct FolderListView: View {
@EnvironmentObject var vm : FolderListViewModel
@Environment(\.presentationMode) var presentationMode
@State private var folderName : String = ""
@State private var showAddSheet : Bool = false
@State private var showAlert : Bool = false
var body: some View {
ZStack{
NavigationView{
ZStack{
if vm.folders.count == 0 {
NoFolderView()
} else {
List {
ForEach(vm.folders) { folder in
NavigationLink(destination: {
MemoListView(folder: folder)
}, label: {
FolderRowView(folder: folder)
})
}
.onDelete(perform: vm.deleteFolder)
}//list
.listStyle(.plain)
.listRowBackground(Color.clear)
}
}
.navigationTitle("Folders ")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.showAddSheet.toggle()
}, label: {
Image(systemName: "plus")
})
}
}
}
HalfASheet(isPresented: $showAddSheet) {
halfSheetView
}
.height(.proportional(0.25))
.contentInsets(EdgeInsets(top: 20, leading: 10, bottom: 10, trailing: 10))
}
.ignoresSafeArea()
}
private var halfSheetView : some View {
VStack(alignment: .leading) {
Text("폴더명을 입력하세요 ")
.font(.headline.bold())
TextField("Insert folder's name", text: $folderName)
.autocapitalization(.none)
.disableAutocorrection(true)
Divider()
.padding(.bottom, 10)
Button(action: {
if vm.checkFolderNameCount(folderName: folderName) {
vm.addNewFolder(folderName: folderName)
self.folderName = ""
self.showAddSheet.toggle()
} else {
self.showAlert.toggle()
}
}, label: {
Text("Save".uppercased())
.font(.headline)
.foregroundColor(.white)
.frame(maxWidth : .infinity)
.frame(height : 50)
.background(Color.accentColor)
.cornerRadius(10)
})
.alert(isPresented : $showAlert) {
Alert(title: Text("Naming Error "), message: Text("Letters for folder's name is too short. Please check it one more time."), dismissButton: .cancel())
}
Spacer()
}//vst
.ignoresSafeArea(.keyboard, edges: .bottom)
.padding()
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
FolderListView()
}
}
此代码首先是关于我的 'halfASheet'(库)代码。
导入 SwiftUI
构造 NewMemoView:视图 {
let folder : FolderModel let memo : MemoModel? @EnvironmentObject var vm : FolderListViewModel @Environment(\.presentationMode) var presentationMode @State private var title : String = "" @State private var content : String = "" @State private var color : Color = .yellow.opacity(0.3) @State private var showAlert : Bool = false @State private var pickedImportance : String = "Easy " @State private var isEditMode : Bool = false let importances = ["Easy ", "Normal ☀️", "Important "] init(memo : MemoModel?, folder : FolderModel) { self.folder = folder self.memo = memo if let memo = memo { _title = State(initialValue: memo.title) _content = State(initialValue: memo.content) _color = State(initialValue: memo.color) _pickedImportance = State(initialValue: memo.isImportant) _isEditMode = State(initialValue: true) } } var body: some View { Form { Section(header : Text("Title ")) { TextField("Input the title", text: $title) .autocapitalization(.none) .disableAutocorrection(true) } Section(header : Text("Importance ✅")) { Picker("", selection: $pickedImportance) { ForEach(importances, id: \.self) { Text([=11=]) } } .pickerStyle(.segmented) } Section(header : Text("Content ✏️")) { TextEditor(text: $content) .autocapitalization(.none) .disableAutocorrection(true) .frame(maxWidth : .infinity) .frame(height : UIScreen.main.bounds.height * 0.3) } Section(header : Text("Select Color")) { ColorPicker("Memo Color", selection: $color, supportsOpacity: false) } Button(action: { if isEditMode { if vm.checkMemoStatus(title: title, content: content) { vm.editMemo(folder: folder, memo: memo!, title: title, content: content, color: color, isImportant: pickedImportance) presentationMode.wrappedValue.dismiss() } else { self.showAlert.toggle() } } else { if vm.checkMemoStatus(title: title, content: content) { vm.addMemo(folder: folder, title: title, content: content, color: color, isImportant: pickedImportance) presentationMode.wrappedValue.dismiss() } else { self.showAlert.toggle() } } }, label: { Text(isEditMode ? "Edit".uppercased() : "save".uppercased()) .fontWeight(.bold) }) .alert(isPresented : $showAlert) { Alert(title: Text("Warning "), message: Text("Check your title and content, please."), dismissButton: .cancel()) } }//form .navigationTitle("Add Memo ") .ignoresSafeArea(.keyboard, edges: .bottom) }
}
接下来的代码是关于带有文本字段和文本编辑器的表单。 (这个截图是关于halfSheet的)
我想做的是,当我点击半张纸的文本字段时, 我想把我的观点推上去。
我试过使用滚动视图,但这不起作用。 我该如何处理这种情况?
谢谢!
您可以在关注文本字段时更改高度值
@State var isEditing = false
TextField("", text: $text, onEditingChanged: { edit in
self.isEditing = edit
})
HalfASheet(isPresented: $showAddSheet) {
halfSheetView
}
.height(.proportional(isEditing ? 0.25 : 0.75))