Skip to content

Instantly share code, notes, and snippets.

View erolando's full-sized avatar
🤠

Rolando Avila erolando

🤠
View GitHub Profile
@simonbs
simonbs / SwiftUIHostingConfiguration.md
Last active September 16, 2024 07:07
In iOS 16, Apple added UIHostingConfiguration, making it straightforward to use a SwiftUI view in instances of UICollectionViewCell and UITableViewCell. This project shows how UIHostingConfiguration can be backported to iOS 14 by implementing a type that conforms to the UIContentConfiguration protocol.

SwiftUIHostingConfiguration

In iOS 16, Apple added UIHostingConfiguration, making it straightforward to use a SwiftUI view in instances of UICollectionViewCell and UITableViewCell. This gist shows how UIHostingConfiguration can be backported to iOS 14 by implementing a type that conforms to UIContentConfiguration.

Starting from iOS 16, we can use SwiftUI views in an instance of UICollectionView or UITableViewCell like this:

cell.contentConfiguration = UIHostingConfiguration {
    ExampleCellContentView(color: Color(item.color), text: item.text)
}
@karigrooms
karigrooms / swiftui-uihostingcontroller-example.swift
Created February 17, 2021 20:50
SwiftUI UIHostingController example for Lessons in SwiftUI blog post
import SwiftUI
import UIKit
class MyViewController: UIViewController {
// this is how you embed a SwiftUI View
lazy var host: UIViewController = {
return UIHostingController(rootView: MySwiftUIView())
}()
@rintoandrews90
rintoandrews90 / xcframework_generate.md
Created December 25, 2020 07:05 — forked from lalkrishna/xcframework_generate.md
Create xcframework instead of Universal(fat) frameworks.

Advantages of xcframework

  • Support for multiple platforms in the same framework.
  • Support for Swift, Objective-C and C.
  • Support for dynamic frameworks and static libraries.
  • Support for Swift Package Manager.
  • End of fat binaries.

Steps to create Aggregate target:

  1. Open Current framework project
@mrackwitz
mrackwitz / AutoHeightLabelView.swift
Created October 27, 2020 19:53
A UILabel-based SwiftUI view which auto-expands to the width of the container, and only expand to render the full height of the label.
struct AutoHeightLabelView: View {
var attributedString: NSAttributedString
var body: some View {
HorizontalGeometryReader { width in
UILabelView(
attributedString: attributedString,
preferredMaxLayoutWidth: width
)
}
@Amzd
Amzd / UIKitTabView.swift
Last active September 4, 2024 06:15
UIKitTabView. SwiftUI tab bar view that respects navigation stacks when tabs are switched (unlike the TabView implementation)
/// An iOS style TabView that doesn't reset it's childrens navigation stacks when tabs are switched.
public struct UIKitTabView: View {
private var viewControllers: [UIHostingController<AnyView>]
private var selectedIndex: Binding<Int>?
@State private var fallbackSelectedIndex: Int = 0
public init(selectedIndex: Binding<Int>? = nil, @TabBuilder _ views: () -> [Tab]) {
self.viewControllers = views().map {
let host = UIHostingController(rootView: $0.view)
host.tabBarItem = $0.barItem
@MarioIannotta
MarioIannotta / Decodable+CustomDateFormat.swift
Last active February 3, 2021 06:07
A little extension to easily decode date with custom formats
import Foundation
var json = """
{
"lastSyncDate": "2018-10-28T09:03:59+0000",
"posts": [
{
"id": 0,
"title": "Swift 4 Codable: tips and tricks",
"link": "https://medium.com/@MarioIannotta/swift-4-codable-tips-and-tricks-7ab4674736d2",
import Foundation
public struct Log
{
public static var isEnabled = true
public static func debug(_ m: @autoclosure ()->String, _ file: Any? = #file, _ f: String = #function, _ line: UInt = #line) {
print(m(), file ?? "", f, line)
}
@gonzalezreal
gonzalezreal / KeyedDecodingContainer+EmptyRepresentable.swift
Last active January 11, 2023 08:25
A technique to avoid having optional array properties in your models when decoding JSON using Swift 4
/// A type that has an "empty" representation.
public protocol EmptyRepresentable {
static func empty() -> Self
}
extension Array: EmptyRepresentable {
public static func empty() -> Array<Element> {
return Array()
}
}
@bnickel
bnickel / Queue Playground.swift
Last active June 23, 2020 16:33
Swift retry operation playground
import Foundation
protocol Completable {
func addCompletionOperation(on queue: OperationQueue, complete: @escaping (Self) -> Void) -> Operation
}
extension Completable where Self: Operation {
func addCompletionOperation(on queue: OperationQueue, complete: @escaping (Self) -> Void) -> Operation {
let completionOperation = BlockOperation {
// Basic.swift
extension Notification.Name {
func post(object:Any? = nil, userInfo:[AnyHashable: Any]? = nil) {
NotificationCenter.default.post(self, object: object, userInfo: userInfo)
}
}
// DataModel.swift
extension Notification.Name {
static let AccountBalanceUpdated = Notification.Name("accountBalanceUpdated")