Skip to content

Instantly share code, notes, and snippets.

View ucargiray's full-sized avatar

Ahmet Giray Uçar ucargiray

View GitHub Profile
@ucargiray
ucargiray / FirstViewController.swift
Last active May 29, 2022 13:51
Example using previews in UIKit
import UIKit
import SwiftUI
class FirstViewController: UIViewController {
lazy var infoLabel: UILabel = {
let view = UILabel()
view.translatesAutoresizingMaskIntoConstraints = false
view.text = "Initial Text"
view.textAlignment = .center
@ucargiray
ucargiray / ViewControllerPreview.swift
Last active May 29, 2022 13:48
Generic ViewController Preview struct
import SwiftUI
struct ViewControllerPreview: UIViewControllerRepresentable {
let viewControllerBuilder: () -> UIViewController
init(_ viewControllerBuilder: @escaping () -> UIViewController) {
self.viewControllerBuilder = viewControllerBuilder
}
import SwiftUI
struct ContentView: View {
@StateObject var userPreference = UserPreferences()
var body: some View {
ZStack() {
Color(userPreference.darkMode ? .black : .white)
.edgesIgnoringSafeArea(.all)
@ucargiray
ucargiray / UserPreferences.swift
Created September 3, 2021 14:16
UserPreferences Model
import Foundation
class UserPreferences : ObservableObject {
@Published var darkMode = false
@Published var notificationOn = false
}
@ucargiray
ucargiray / ContentView.swift
Created July 31, 2021 13:28
An example for AddCustomWebsiteExtension usage.
@AddCustomWebsiteExtension var website = "giray"
@ucargiray
ucargiray / WebsitePropertyWrapper.swift
Created July 31, 2021 13:24
Custom Property Wrapper for Website.
@propertyWrapper struct AddCustomWebsiteExtension {
var wrappedValue : String {
didSet {
if !(wrappedValue.hasPrefix("https://") || wrappedValue.hasSuffix(".dev")) {
self.wrappedValue = "https://" + wrappedValue + ".dev"
}
}
}
init(wrappedValue: String) {
if wrappedValue.hasPrefix("https://") || wrappedValue.hasSuffix(".dev") {
@ucargiray
ucargiray / ContentView.swift
Created July 31, 2021 12:48
An example for SwiftUI property change.
import SwiftUI
struct ContentView: View {
@State private var infoString : String?
var body: some View {
VStack {
Text(infoString ?? "No name")
Button("Change text") {
@ucargiray
ucargiray / ViewController.swift
Created July 31, 2021 12:45
An example for UIKit property change.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var infoLabel: UILabel!
var infoString : String?
override func viewDidLoad() {
super.viewDidLoad()
infoLabel.text = "Property Wrappers"
}
@ucargiray
ucargiray / ContentView.swift
Last active September 3, 2021 14:08
Second example for @binding property wrapper.
import SwiftUI
struct ContentView: View {
@State var text = ""
var body: some View {
VStack {
TextField("Type here", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
@ucargiray
ucargiray / ContentView.swift
Last active September 3, 2021 14:01
First example for @binding property wrapper.
import SwiftUI
struct ContentView: View {
@State var isOn = false
var body: some View {
ZStack {
ContentView2(isOnFromContentView: $isOn)
Button("Flip color") {