У меня есть ContentView (тот, с которым возникла проблема), который содержит пользовательскую панель навигации и несколько представлений.
Упрощенный код ниже.
Мой ContentView:
Код: Выделить всё
struct ContentView: View {
var body: some View {
CustomNavBarContainerPreview {
ZStack {
Color(.purple)
.opacity(0.4)
.ignoresSafeArea(.all)
VStack(alignment: .center) {
//view with textfield
PullView()
RandomView()
LanguageView()
Button {
//action
} label: {
ZStack {
RoundedRectangle(cornerRadius: 15)
.foregroundColor(.cyan)
.frame(minWidth: 300, idealWidth: 340, maxWidth: 340, minHeight: 70, idealHeight: 85, maxHeight: 85, alignment: .center)
Text("Accept Settings")
.font(Font.custom("Ubuntu-Regular", size: 24))
.foregroundColor(.white)
}
.padding(.vertical, 10)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
//using this isn't prevent the view from pusing up
.ignoresSafeArea(.keyboard, edges: .bottom)
}
// it doesn't work too
.ignoresSafeArea(.keyboard)
.onTapGesture {
dismissKeyboard()
}
}
}
func dismissKeyboard() {
UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.endEditing(true)
}
}
Код: Выделить всё
struct PullView: View {
@State var num1: String = ""
@State var num2: String = ""
var body: some View {
ZStack(alignment: .topLeading) {
Rectangle()
.frame(minWidth: 342, idealWidth: 342, maxWidth: 342, minHeight: 150, idealHeight: 178, maxHeight: 178, alignment: .center)
.foregroundColor(.white)
VStack {
HStack {
Text("Title One")
.bold()
.multilineTextAlignment(.leading)
.foregroundColor(.black)
.padding(.leading, 15)
Spacer()
Image(systemName: "info.circle.fill")
.scaledToFill()
.frame(alignment: .trailing)
.padding(.trailing, 15)
}
.frame(maxWidth: 342, maxHeight: 50)
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
.background(Rectangle()
.foregroundColor(Color(red: 230/255, green: 234/255, blue: 242/255))
.opacity(0.5))
HStack(alignment: .center) {
Text("field1")
.foregroundColor(.black)
TextField("", value: $num1, formatter: NumberFormatter())
.textFieldStyle(DefaultTextFieldStyle())
.foregroundColor(.black)
.multilineTextAlignment(.center)
.frame(width: 90, height: 48)
.keyboardType(.numberPad)
.background(.green)
Text("field2")
.foregroundColor(.black)
TextField("", value: $num2, formatter: NumberFormatter())
.textFieldStyle(DefaultTextFieldStyle())
.foregroundColor(.black)
.multilineTextAlignment(.center)
.frame(width: 90, height: 48)
.keyboardType(.numberPad)
.background(.green)
}
.padding(.all, 10)
}
}
}
}
struct RandomView: View {
var body: some View {
ZStack(alignment: .topLeading) {
Rectangle()
.frame(minWidth: 342, idealWidth: 342, maxWidth: 342, minHeight: 150, idealHeight: 178, maxHeight: 178, alignment: .center)
.foregroundColor(.white)
VStack {
HStack {
Text("Title Two")
.bold()
.multilineTextAlignment(.leading)
.foregroundColor(.black)
.padding(.leading, 15)
Spacer()
Image(systemName: "info.circle.fill")
.scaledToFill()
.frame(alignment: .trailing)
.padding(.trailing, 15)
}
.frame(maxWidth: 342, maxHeight: 50)
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
.background(Rectangle()
.foregroundColor(Color(red: 230/255, green: 234/255, blue: 242/255))
.opacity(0.5))
Text("Random settings")
Text("Subview test")
}
}
}
}
struct LanguageView: View {
var body: some View {
ZStack(alignment: .topLeading) {
Rectangle()
.frame(minWidth: 342, idealWidth: 342, maxWidth: 342, minHeight: 150, idealHeight: 178, maxHeight: 178, alignment: .center)
.foregroundColor(.white)
VStack {
HStack {
Text("Title Three")
.bold()
.multilineTextAlignment(.leading)
.foregroundColor(.black)
.padding(.leading, 15)
Spacer()
Image(systemName: "info.circle.fill")
.scaledToFill()
.frame(alignment: .trailing)
.padding(.trailing, 15)
}
.frame(maxWidth: 342, maxHeight: 50)
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
.background(Rectangle()
.foregroundColor(Color(red: 230/255, green: 234/255, blue: 242/255))
.opacity(0.5))
Text("Language settings")
Text("Subview test")
}
}
}
}
Код: Выделить всё
struct CustomNavBar: View {
var body: some View {
ZStack {
Rectangle()
.foregroundColor(.clear)
.frame(width: .infinity, height: 70)
.background(.mint)
HStack {
Button("Back") {
}
Spacer()
Text("Heading")
.bold()
.foregroundColor(.white)
Spacer()
Button("Next") {
}
}
.padding(.horizontal)
}
.navigationBarHidden(true)
}
}
struct CustomNavBarContainerPreview: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
VStack(spacing: 0) {
CustomNavBar()
content
}
}
}
Готов к тестированию
В чем может быть проблема? Как это исправить?
Я порылся в StackOverflow и не нашел ответа. Я видел, что это распространенная ошибка в SwiftUI.
Я пытался вставить .ignoresSafeArea(.keyboard) и .ignoresSafeArea(.keyboard, Edges: .bottom) везде в моем коде ниже ( в ContentView, SettingsViews, пользовательской панели навигации), чтобы использовать Spacers() и .ignoresSafeArea(.all), и это не дало никаких результатов: представление все еще движется.
Также Я использовал хак с ScrollView, чтобы исправить это, но у меня он сработал неправильно.
Может быть, я могу встроить код UIKit для управления представлением и клавиатурой? Но я не знаю, как это сделать.
Подробнее здесь: https://stackoverflow.com/questions/769 ... safearea-k