Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Last active February 26, 2020 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perlguy99/7d2e6654217481ba0b49ef3616fe4d8e to your computer and use it in GitHub Desktop.
Save perlguy99/7d2e6654217481ba0b49ef3616fe4d8e to your computer and use it in GitHub Desktop.
SwiftUI Buttons & TextFields

SwiftUI Button & TextField Info

First off, an excellent resource is

Mastering Buttons in SwiftUI

TextField in SwiftUI

Don't forget to use the Environment Object if possible!

Some Button basics...

import UIKit
import SwiftUI
import PlaygroundSupport

var str = "Hello, playground"

struct FilledButton: ButtonStyle {
    
    func makeBody(configuration: Configuration) -> some View {
        configuration
            .label
            .foregroundColor(configuration.isPressed ? .gray : .white)
            .padding()
            .background(Color.accentColor)
            .cornerRadius(8)
    }
}


struct RootView: View {
    var body: some View {
        
        VStack {
            
            Button(action: {
                print("Button 1 tapped")
            }) {
                Text("Butt 1")
            }
        .buttonStyle(FilledButton())
        }
    }
}


PlaygroundPage.current.setLiveView(RootView())

Some TextField Basics...

import UIKit
import SwiftUI
import PlaygroundSupport

var str = "Hello, playground"

extension NumberFormatter {
    static var currency: NumberFormatter {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        return formatter
    }
}


struct SuperCustomTextFieldStyle: TextFieldStyle {
    func _body(configuration: TextField<_Label>) -> some View {
        configuration
            .padding()
            .border(Color.accentColor)
    }
}


struct ContentView: View {
    @State private var text  = ""
    @State private var price = 99
    
    var body: some View {
        
        TextField("type something...", text: $text)
            .textFieldStyle(SuperCustomTextFieldStyle())

            .padding()

        
    }
}

PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment