描述:
我目前正在开发一个受 Apple 地图启发的 SwiftUI 应用程序,我在主视图上有一个主工作表,背景设置为
.ultraThinMaterial.
该工作表在大多数情况下都运行良好,但我遇到了一个问题,尤其是打开 CarDetailsSheet
时,.ultraThinMaterial
背景并未一致应用,而是出现白色背景(随机)。
// Code for presenting the primary sheet
.sheet(isPresented: .constant(true)) {
BottomSheet()
.presentationDetents([.medium, .large, .height(geometry.safeAreaInsets.bottom + 32)])
.presentationBackgroundInteraction(.enabled)
.presentationBackground(.ultraThinMaterial)
.interactiveDismissDisabled()
}
// Code for the BottomSheet
struct BottomSheet: View {
// ... (omitting some code for brevity)
// Sheet for presenting details
.sheet(item: $selectedCar) { car in
VStack {
Spacer()
CarDetailsSheet(car.id)
.padding()
.presentationBackground(.ultraThinMaterial)
}
}
// ... (omitting some code for brevity)
}
我尝试了各种方法,例如调整修饰符的顺序和删除不必要的背景修饰符,但问题仍然存在。具体来说,当打开 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
嘿,我遇到了同样的问题,我最终通过创建视图扩展的旧方法来修复它:在扩展内放置此函数:
func blurredSheet<Content: View> (_ style: AnyShapeStyle, show: Binding<Bool>, onDismiss:
@escaping ()->(), @ViewBuilder content: @escaping ()->Content)->some View{
self
.sheet(isPresented: show, onDismiss: onDismiss) {
content ()
.background(RemovebackgroundColor())
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background {
Rectangle()
.fill (style)
.ignoresSafeArea(.container, edges: .all)
}
}
}
你还需要这个:
fileprivate struct RemovebackgroundColor: UIViewRepresentable{
func makeUIView(context: Context) -> UIView {
return UIView()
}
func updateUIView(_ uiview: UIView, context: Context) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
uiview.superview?.superview?.backgroundColor = .clear
}
}
} 祝你好运