Swift ui macos background transparent TextField

后端 未结 2 402
醉话见心
醉话见心 2021-01-15 02:24

As you can see from the image I have a TextField and after list.

The list has a transparent background, I\'m using .listStyle(

相关标签:
2条回答
  • 2021-01-15 02:53

    You could use on the background Color.white.opacity(0.01) or Color.clear in your case:

         .background(
           RoundedRectangle(cornerRadius: 5)
           .fill(Color.white.opacity(0.01))
         
    

    or

          .background(
            RoundedRectangle(cornerRadius: 5)
            .fill(Color.clear))
       
    
    0 讨论(0)
  • 2021-01-15 03:05

    You need visual effect view in background (it is used by default for sidebar styled lists)

    Demo prepared & tested with Xcode 11.4 / macOS 10.15.6

    struct VisualEffectView: NSViewRepresentable {
        func makeNSView(context: Context) -> NSVisualEffectView {
            let view = NSVisualEffectView()
    
            view.blendingMode = .behindWindow    // << important !!
            view.isEmphasized = true
            view.material = .appearanceBased
            return view
        }
    
        func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
        }
    }
    

    and put it to needed area, in this case below TextField

        TextField("Username", text: $username)
        .padding(.leading, 20)
        .padding(.trailing, 20)
        .background(
            RoundedRectangle(cornerRadius: 5)
                .fill(Color.white.opacity(0.3)
            )
            .padding(.leading, 20)
            .padding(.trailing, 20)
        )
        .padding(.top)
        .padding(.bottom)
        .background(VisualEffectView())
    
    0 讨论(0)
提交回复
热议问题