Swift UI MACos Background Transparent Textfield

Swift ui macos background transparent TextField

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

demo

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())

SwiftUI/macOS: DatePicker without background field

Here is a simplified demo of representable wrapper approach

Tested with Xcode 13.2 / macOS 12.1

Sample Image

struct DemoView: View {
@State private var newDate: Date = Date()

var body: some View {
VStack {
Text("Selected: \(newDate)")
MyDatePicker(selection: $newDate)
}
}
}

struct MyDatePicker: NSViewRepresentable {
@Binding var selection: Date

func makeNSView(context: Context) -> NSDatePicker {
let picker = NSDatePicker()
picker.isBordered = false
picker.datePickerStyle = .textField
picker.action = #selector(Coordinator.onValueChange(_:))
picker.target = context.coordinator
return picker
}

func updateNSView(_ picker: NSDatePicker, context: Context) {
picker.dateValue = selection
}

func makeCoordinator() -> Coordinator {
Coordinator(owner: self)
}

class Coordinator: NSObject {
private let owner: MyDatePicker
init(owner: MyDatePicker) {
self.owner = owner
}

@objc func onValueChange(_ sender: Any?) {
if let picker = sender as? NSDatePicker {
owner.selection = picker.dateValue
}
}
}
}

Changing TextEditor background color in SwiftUI for macOS

I have just posted an answer for that issue on a similar question here

With the help of extension, you can clear the default background Color of the NSTextView class and then use .background modifier in SwiftUI like this

extension NSTextView {
open override var frame: CGRect {
didSet {
backgroundColor = .clear //<<here clear
drawsBackground = true
}

}
}

struct ContentView: View {

@State var string: String = ""

var body: some View {
TextEditor(text: $string)
.textFieldStyle(PlainTextFieldStyle())
.background(Color.red) //<< here red
}
}

SwiftUI: How to set background color for textfield

There is no need to use UIAppearance to set the background color of the ScrollView; You can use use .background modifier on it.

UIAppearance makes global changes and can have unintended consequences.

How can I make a background in SwiftUI translucent?

SwiftUI's Color has an opacity() function that returns another Color with the given opacity. An opacity of 1.0 would be the same color, while an opacity of 0.0 would be completely clear.

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

Transparent Background for TextEditor in SwiftUI

Here is a possible work around for this. The extension sets all of your TextViews background to .clear and then you are able to change the background color with .background() modifier.

import SwiftUI

extension NSTextView {
open override var frame: CGRect {
didSet {
backgroundColor = .clear //<<here clear
drawsBackground = true
}

}
}

struct ContentView: View {

@State var string: String = ""

var body: some View {
TextEditor(text: $string)
.textFieldStyle(PlainTextFieldStyle())
.background(Color.red) //<< here red
}
}

Sample Image

SwiftUI Textfield - How to set the background Color for textfield in edit mode to clear

"UIScrollView.appearance().backgroundColor = .lightGray" in the init method of my rootview does also effect the Textfield behavior. Deleting this line did the trick.



Related Topics



Leave a reply



Submit