Я создаю приложение iPhone с SwiftUI и сталкиваюсь с проблемой макета: когда я тестирую свое приложение на устройстве или на симуляторе, в ландшафтном режиме происходит следующее: нижняя панель перекрывает последний элемент списка (отмечен красным на снимок экрана):
![FirstView tab](https://i.stack.imgur.com/cQORd.png)
Мое желание никогда не допускать этого, поэтому результат должен выглядеть следующим образом (отмечен зеленым на скриншоте):
![SecondView tab](https://i.stack.imgur.com/Ofe1L.png)
Подробная информация о приложении: на основе шаблона приложения Xcode iOS с вкладками приложения. 3 просмотра: ContentView.swift
, FirstView.swift
, SecondView.swift
. Я добавил фиктивный список в FirstView.swift
, чтобы проиллюстрировать проблему.
ContentView.swift
код:
struct ContentView: View {
@State private var selection = 0
var body: some View {
TabView(selection: $selection){
FirstView()
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
SecondView()
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
}
}
}
FirstView.swift
код:
import SwiftUI
struct FirstView: View {
var body: some View {
NavigationView {
List(0..<10) { item in
Image(systemName: "photo")
VStack(alignment: .leading) {
Text("Hello")
}
}
.navigationBarTitle("First View Title")
}
}
}
SecondView.swift
code:
import SwiftUI
struct SecondView: View {
var body: some View {
NavigationView {
Text("Second View")
.navigationBarTitle("Second View Title")
}
}
}
Я нашел обходной путь, но это не то, как я хочу, чтобы он работал: если я перевожу устройство в альбомный режим, нажмите вкладку SecondView, а затем нажмите FirstView, нажмите и прокрутите список сверху вниз панель вкладок не перекрывает последний элемент списка. Есть ли способ исправить перекрытие TabView последнего элемента списка в ландшафтном режиме? Спасибо.
UPD : добавлено func scene()
из SceneDelegate.swift
:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Get the managed object context from the shared persistent container.
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let contentView = ContentView().environment(\.managedObjectContext, context)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}