我需要忽略TabView
的安全区域,这样不同页面中的ScrollView
就会在安全区域之外显示可滚动的内容。
当容器的安全区域发生变化时(例如,当显示键盘时),视图将被重新绘制,其选择状态丢失。
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.
复制代码:
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)
可以阻止视图的选择丢失,但也会失去ScrollView
在安全区域之外显示其内容的能力。
我做错什么了吗,还是SwiftUI出了问题?在用Xcode 14构建iOS 16之前,我没有注意到它,所以有一些变化或者是坏了:D。
发布于 2022-11-13 05:25:00
苹果已经证实,这是Xcode 14的回归,Xcode 14现在已经修正了Xcode 14.1。
https://stackoverflow.com/questions/74012744
复制相似问题