Skip to content

Instantly share code, notes, and snippets.

extension UIApplication {
func addTapGestureRecognizer() {
guard let window = windows.first else { return }
let tapGesture = UITapGestureRecognizer(target: window, action: #selector(UIView.endEditing))
tapGesture.cancelsTouchesInView = false
tapGesture.delegate = self
tapGesture.name = "MyTapGesture"
window.addGestureRecognizer(tapGesture)
}
}
extension UIApplication: UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return false // set to `false` if you don't want to detect tap during other gestures
}
}
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
}
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
@jbadger3
jbadger3 / NavigableListButtonFix.swift
Last active November 19, 2020 19:10
NavigableListWithInteractiveCells
Button(action: {
currentFavorite = teamMember
}, label: {
Image(systemName: (teamMember == currentFavorite ? "heart.fill" : "heart"))
.foregroundColor(.pink)
})
.buttonStyle(PlainButtonStyle())
@jbadger3
jbadger3 / SwiftUI_overlapping_navigation_titles_content_view.swift
Created December 30, 2020 15:56
SwiftUI: Overlapping Navigation Titles
struct FoodGroup {
var name: String
var examples: [String]
}
struct ContentView: View {
var foodGroups: [FoodGroup] {
let fruits = FoodGroup(name: "Fruit", examples: ["Apple", "Banana", "Pear", "Peach", "Mango", "Orange", "Strawberry", "Watermelon", "Pineapple", "Lemon", "Lime", "Cherry", "Date", "Plum", "Apricot", "Blueberry", "Blackberry", "Cranberry", "Kiwi", "Nectarine"])
let vegetables = FoodGroup(name: "Vegetables", examples: ["Lettuce", "Carrot", "Beet", "Broccoli", "Corn", "Celery", "Chicory", "Kale", "Spinach", "Yarrow", "Brussels sprouts", "Arugula", "Cauliflower", "Turnip", "Sweet Potato"])
return [fruits, vegetables]
@jbadger3
jbadger3 / NumericTextField.swift
Last active April 13, 2021 14:55
Working with TextField Views in SwiftUI
struct NumericTextField<T>: UIViewRepresentable {
private var title: String
@Binding var value: T
private var formatter: NumberFormatter
@State var errorMessage = ""
private var keyboardType: UIKeyboardType
init(title: String = "", value: Binding<T>, numberFormatter: NumberFormatter, keyboardType: UIKeyboardType) {
self.title = title
self._value = value
@jbadger3
jbadger3 / parsing_character_entities_NSAttributedString.swift
Last active March 19, 2024 15:24
Gist for the article 'Parsing Character Entities from HTML/XML Content In Swift' on Medium.com
extension String {
init?(htmlEncodedString: String) {
guard let data = htmlEncodedString.data(using: .utf8) else {
return nil
}
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else {
@jbadger3
jbadger3 / PostData.swift
Last active October 1, 2021 22:17
Gists for the Medium post 'Paving A Road Between JSON And CoreData'
struct PostData: Decodable {
let userId: Int
let id: Int
let title: String
let body: String
}
@jbadger3
jbadger3 / swiftncurses_package_example.swift
Created September 9, 2022 16:09
terminus_medium_article
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
...
let package = Package(
name: "SwiftNCurses",
products: [,
targets: [