SwiftUI Sheet中.ultraThinMaterial背景随机失效问题求助
SwiftUI Sheet 背景随机失效问题
问题场景
开发参考Apple Maps的SwiftUI应用时,首页主Sheet设置了.ultraThinMaterial背景,多数情况下显示正常,但打开CarDetailsSheet时,主Sheet背景会随机失效,变为白色。
相关代码
主Sheet展示代码
.sheet(isPresented: .constant(true)) { BottomSheet() .presentationDetents([.medium, .large, .height(geometry.safeAreaInsets.bottom + 32)]) .presentationBackgroundInteraction(.enabled) .presentationBackground(.ultraThinMaterial) .interactiveDismissDisabled() }
BottomSheet相关代码
struct BottomSheet: View { // ...(省略部分代码) // 详情Sheet展示 .sheet(item: $selectedCar) { car in VStack { Spacer() CarDetailsSheet(car.id) .padding() .presentationBackground(.ultraThinMaterial) } } // ...(省略部分代码) }
已尝试操作
调整修饰符顺序、移除多余背景修饰符,问题仍未解决:打开CarDetailsSheet时,背景有时显示正常的.ultraThinMaterial,有时变为白色。
视觉对比
- 预期背景:

- 实际异常背景:

关闭CarDetailsSheet时的日志
Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x2838ed3b0 'accessoryView.bottom' _UIRemoteKeyboardPlaceholderView:0x131448960.bottom == _UIKBCompatInputView:0x13d61f7c0.top (active)>", "<NSLayoutConstraint:0x28383d9a0 'assistantHeight' SystemInputAssistantView.height == 45 (active, names: SystemInputAssistantView:0x13d61b0e0 )>", "<NSLayoutConstraint:0x2838c30c0 'assistantView.bottom' SystemInputAssistantView.bottom == _UIKBCompatInputView:0x13d61f7c0.top (active, names: SystemInputAssistantView:0x13d61b0e0 )>", "<NSLayoutConstraint:0x2838c2bc0 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x131448960]-(0)-[SystemInputAssistantView] (active, names: SystemInputAssistantView:0x13d61b0e0 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x2838c2bc0 'assistantView.top' V:[_UIRemoteKeyboardPlaceholderView:0x131448960]-(0)-[SystemInputAssistantView] (active, names: SystemInputAssistantView:0x13d61b0e0 )> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
环境信息
- Xcode版本:15.1 (15C65)
- iOS版本:17.2
解决建议
- 优化嵌套Sheet布局层级:iOS 17的嵌套Sheet在处理
presentationBackground时有随机渲染bug,先移除内层Sheet外层的VStack和Spacer,直接给CarDetailsSheet添加背景修饰符,减少布局层级干扰:.sheet(item: $selectedCar) { car in CarDetailsSheet(car.id) .padding() .presentationBackground(.ultraThinMaterial) } - 强制主Sheet刷新:关闭内层Sheet后,通过更新主Sheet的
id触发重新渲染,确保背景恢复:.sheet(isPresented: .constant(true)) { BottomSheet(selectedCar: $selectedCar) // 保留原有修饰符 .id(selectedCar?.id ?? UUID()) } - 处理键盘约束冲突:日志中的约束问题和键盘占位视图相关,若
CarDetailsSheet包含输入控件,在其消失时强制收起键盘,避免干扰布局:
在CarDetailsSheet中添加:.onDisappear { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } - 尝试系统版本调整:该问题大概率是iOS 17.2的系统bug,可升级到iOS 17.3及以上版本,或回退到iOS 17.1,验证问题是否解决。
内容的提问来源于stack exchange,提问作者raouf Mehdid
相关产品推荐
相关产品推荐

