ios - 当容器大小发生变化时,考虑到安全区域被忽略,SwiftUI TabView 会失去选择

标签 ios xcode swiftui ios16 swiftui-tabview

我需要忽略 TabView 的安全区域,以便不同页面中的 ScrollView 能够在安全区域之外显示其可滚动内容。

当容器的安全区域发生变化时,例如当显示键盘时, View 将被重绘并且其选择状态会丢失。

2022-10-10 17:18:13.762064+0800 SafeArea[73226:7824972] The behavior of the UICollectionViewFlowLayout is not defined because:
2022-10-10 17:18:13.762259+0800 SafeArea[73226:7824972] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2022-10-10 17:18:13.762567+0800 SafeArea[73226:7824972] The relevant UICollectionViewFlowLayout instance is <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x7f8509c0a680>, and it is attached to <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF020PagingCollectionView: 0x7f850b037400; baseClass = UICollectionView; frame = (0 0; 393 612.667); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000009fa550>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000007d4ac0>; contentOffset: {0, 0}; contentSize: {786, 726.33333333333337}; adjustedContentInset: {0, 0, 21.267423644046971, 0}; layout: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF012PagingLayout: 0x7f8509c0a680>; dataSource: <_TtC7SwiftUIP33_8825076C2763A50452A210CBE1FA4AF011Coordinator: 0x60000359c5a0>>.
2022-10-10 17:18:13.763934+0800 SafeArea[73226:7824972] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

enter image description here

重现代码:

import SwiftUI

struct ContentView: View {
    
    @State var selection = 0
    
    var body: some View {
        VStack {
            Text("An app")
            TabView(selection: $selection) {
                ScrollView {
                    TextField("First textfield", text: .constant(""))
                }
                .background(.blue)
                .tag(0)
                
                ScrollView {
                    TextField("Second textfield", text: .constant(""))
                }
                .background(.red)
                .tag(1)
            }
            .tabViewStyle(.page)
            .ignoresSafeArea(.container, edges: .bottom)
        }
    }
}

只需删除 .ignoresSafeArea(.container, Edges: .bottom) 即可防止 View 的选择丢失,但也会失去 ScrollView 显示其内容的能力超出安全区域。

我做错了什么还是 SwiftUI 的问题?在使用 Xcode 14 针对 iOS 16 进行构建之前,我没有注意到这一点,所以有些东西发生了变化,或者有些东西被破坏了:D。

最佳答案

Apple 已确认这是 Xcode 14 中的回归,现已在 Xcode 14.1 中修复。

https://feedbackassistant.apple.com/feedback/11684856

关于ios - 当容器大小发生变化时,考虑到安全区域被忽略,SwiftUI TabView 会失去选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74012744/

相关文章:

iOS - 从 NSUserDefaults 调用特定键盘

ios - Xcode 7.1 "The request timed out."错误

ios - 源代码控制 xCode 6 和现有文件

ios - XCODE/SWIFT - 将 ADMOB 横幅重新定位到底部

swiftUI 如何在键盘上设置搜索按钮

ios - 无法使用 NSJSONSerialization 解析 JSON

ios - 无法指定经纬度方向

iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource'

ios - SwiftUI:添加核心数据项时更新选项卡栏角标(Badge)

SwiftUI 为什么这个 API 调用对我不起作用?