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

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

【问题讨论】:

    标签: swiftui


    【解决方案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://stackoverflow.com/a/61446820/12299030 中的示例

    【讨论】:

    • 感谢您的快速回复!那么您是说除非全屏显示工作表,否则无法修改此默认行为?
    猜你喜欢
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2019-10-30
    • 2020-10-11
    • 1970-01-01
    • 2020-05-23
    • 2014-03-12
    相关资源
    最近更新 更多