my name
my name

Reputation: 51

SwiftUI TextField and overlay

I want to add an overlay view (Color.black) to a textField (goal is to reduce brightness).

However, after I add the overlay view, textField is unable to use(select).

I tried allowsHitTesting(false), but no use.

Does anyone know how to solve the problem?

TextField(title: StringProtocol, text: Binding<String>)
    .overlay(Color.black.opacity(0.2))

Upvotes: 1

Views: 2744

Answers (2)

user3441734
user3441734

Reputation: 17534

to reduce brightness, use .brightness modifier!

TextField("title", text: $txt).brightness(0.5)

Generally, combination of .brightness, .opacity, .foregroundColor, .background helps you to adjust the visual appearance for your needs.

To change cursor style use .accentColor

Upvotes: 1

Felix Marianayagam
Felix Marianayagam

Reputation: 2714

Overlays create a secondary view in front of the TextField. You can achieve the same result using background. Here's the code. Hope this helps.

import SwiftUI

struct ContentView: View {
    @State var name: String = "This is a test."
    var body: some View {
        TextFieldView(name: $name)
    }
}

struct TextFieldView: View {
    @Binding var name: String
    var body: some View {
        TextField("Name", text: $name)
            .background(
                Color.black.opacity(0.2)
            )
    }
}

Upvotes: 1

Related Questions