简体   繁体   English

如何使 SwiftUI 中的背景半透明?

[英]How can I make a background in SwiftUI translucent?

How can I make the text background above the navigation bar translucent so that it looks like the text and navigation bar are the same object?如何使导航栏上方的文本背景为半透明,使文本和导航栏看起来像 object?

VStack(spacing: 0) {
    Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color.gray) // I used a custom color set in the screenshot
        .font(.footnote)
    NavigationView {
        List {
            Text("Name")
            Text("Name")
            Text("Name")
        } .listStyle(GroupedListStyle())

        .navigationBarTitle(Text(""), displayMode: .inline)
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(
            leading:
            Button("Cancel") {
                // self.presentationMode.wrappedValue.dismiss()
            },
            trailing:
            Button("Done") {

            }.disabled(true)
        )
    }
}

在此处输入图像描述

SwiftUI's Color has an opacity() function that returns another Color with the given opacity. SwiftUI 的Color有一个opacity() function 返回另一个具有给定不透明度的Color An opacity of 1.0 would be the same color, while an opacity of 0.0 would be completely clear.不透明度 1.0 将是相同的颜色,而不透明度 0.0 将完全清晰。

For example, if you wanted to have the color be between completely opaque and completely clear, change:例如,如果您想让颜色介于完全不透明和完全透明之间,请更改:

Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color.gray)
        .font(.footnote)

To:至:

Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color.gray.opacity(0.5)) //Here is where we use the opacity
        .font(.footnote)

Source: https://developer.apple.com/documentation/swiftui/color资料来源: https://developer.apple.com/documentation/swiftui/color

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使UIPageViewController背景半透明? - How to make UIPageViewController background translucent? SwiftUI:fullScreenCover 的半透明背景 - SwiftUI: Translucent background for fullScreenCover 如何在iOS 7中将用户背景添加到应用程序中作为半透明背景? - How can I add the user background into the app as a translucent background in iOS 7? 为什么黑色半透明UIToolbar的顶部与黑色半透明UISearchBar的顶部不同,如何使它们看起来相同? - Why is the top of a black translucent UIToolbar different from a black translucent UISearchBar, and how can I make them look the same? 如何使 SwiftUI 列表行背景颜色扩展到整个宽度,包括安全区域之外 - How can I make a SwiftUI List row background color extend the full width, including outside of the safe area 如何使背景视图半透明到iPad的主屏幕 - How to make Background view translucent to home screen of ipad 如何在 SwiftUI 中使 TextAlignment 可编码? - How can I make TextAlignment Codable in SwiftUI? iOS,我不能用UIViewcontroller和UIWindow完成半透明的背景色吗? - iOS ,I can't finish the background color translucent with UIViewcontroller and UIWindow? 如何在 SwiftUI 上使用 CAEmitterLayer 制作 animation? - How can I make animation with CAEmitterLayer on SwiftUI? SwiftUI - 如何模糊视图的默认背景颜色? - SwiftUI - How can I blur the default background color of a view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM