如何防止工作表在 SwiftUI 中使其后面的视图变暗或删除不必要的填充?

Posted

技术标签:

【中文标题】如何防止工作表在 SwiftUI 中使其后面的视图变暗或删除不必要的填充?【英文标题】:How do I prevent a sheet from darkening the view behind it in SwiftUI or remove the unnecessary padding? 【发布时间】:2020-10-08 00:31:05 【问题描述】:

当一个工作表(模态)出现时,它后面的视图会缩小并变暗,导致工作表后面出现一个灰色的矩形(见图中红色框内的位)。如何防止背景中的视图变暗,或者在它缩小时消除它周围的白色填充?

【问题讨论】:

【参考方案1】:

这是默认的.sheet 行为。

使用 SwiftUI 2.0 改为使用全屏覆盖

@available(ios 14.0, tvOS 14.0, watchOS 7.0, *)
@available(macOS, unavailable)
extension View 

    /// Presents a modal view that covers as much of the screen as
    /// possible using the given item as a data source for the sheet's content.
    ///
    /// - Parameters:
    ///   - item: A binding to an optional source of truth for the cover
    ///     modal view. When representing a non-nil item, the system uses
    ///     `content` to create a modal representation of the item.
    ///     If the identity of `item` changes, the system will dismiss a
    ///     currently-presented modal view and replace it by a new modal view.
    ///   - onDismiss: A closure executed when the modal view dismisses.
    ///   - content: A closure returning the content of the modal view.
    public func fullScreenCover<Item, Content>(item: Binding<Item?>, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping (Item) -> Content) -> some View where Item : Identifiable, Content : View


    /// Presents a modal view that covers as much of the screen as
    /// possible when a given condition is true.
    ///
    /// - Parameters:
    ///   - isPresented: A binding to whether the modal view is presented.
    ///   - onDismiss: A closure executed when the modal view dismisses.
    ///   - content: A closure returning the content of the modal view.
    public func fullScreenCover<Content>(isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content) -> some View where Content : View


对于 SwiftUI 1.0,您可以使用过渡来全屏显示您的视图,请参阅 https://***.com/a/61446820/12299030 中的示例

【讨论】:

感谢您的快速回复!那么您是说除非全屏显示工作表,否则无法修改此默认行为?

以上是关于如何防止工作表在 SwiftUI 中使其后面的视图变暗或删除不必要的填充?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 SwiftUI 中使视图的高度从 0 动画到高度?

SwiftUI 模式表在关闭后重新打开

如何在 SwiftUI 中使 UIViewRepresentable 在 tvOS 上具有焦点?

SwiftUI如何防止视图重新加载整个身体

如何防止键盘在 SwiftUI 中向上推视图? [复制]

SwiftUI:自定义模态动画