Skip to content

Instantly share code, notes, and snippets.

View OscarApeland's full-sized avatar

Oscar Apeland OscarApeland

  • Oslo
  • 08:48 (UTC +01:00)
View GitHub Profile
@OscarApeland
OscarApeland / DynamicRepro.swift
Created June 13, 2023 09:54
An isolated reproduction of issues managing offset manually while using UIKit Dynamics
//
// ContentView.swift
// DynamicRepro
//
// Created by Oscar Apeland on 13/06/2023.
//
import SwiftUI
import UIKit
protocol CustomSubviewDelegate: class {
func customSubview(_ customSubview: CustomSubview, didEnterText text: String)
}
class CustomSubview: UIView {
weak var delegate: CustomSubviewDelegate?
// init and setup and stuff
@objc func textFieldDidChange(_ textField: UITextField) {
extension UIView {
func edgeConstraints(to view: UIView, insets: UIEdgeInsets = .zero) -> [NSLayoutConstraint] {
return [
topAnchor.constraint(equalTo: view.topAnchor, constant: insets.top),
leftAnchor.constraint(equalTo: view.leftAnchor, constant: insets.left),
rightAnchor.constraint(equalTo: view.rightAnchor, constant: -insets.right),
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -insets.bottom)
]
}
import UIKit
extension UICollectionView {
func register<CellClass: UICollectionReusableView>(_ cellClass: CellClass.Type, ofKind kind: String? = nil) {
let cellId = String(describing: type(of: cellClass.classForCoder()))
if let kind = kind {
register(cellClass, forSupplementaryViewOfKind: kind, withReuseIdentifier: cellId)
} else if let collectionViewCellClass = cellClass as? UICollectionViewCell.Type {
register(collectionViewCellClass, forCellWithReuseIdentifier: cellId)
@OscarApeland
OscarApeland / KeyboardOffset.swift
Created March 6, 2018 10:55
How to offset and animate something that hovers above the keyboard with auto layout constraints
NotificationCenter.default.addObserver(forName: .UIKeyboardWillChangeFrame, object: nil, queue: .main) { [weak self] (notification) in
guard let strongSelf = self,
let targetFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect,
let animationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber,
let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber else {
return
}
strongSelf.view.layoutIfNeeded()
strongSelf.proceedButtonBottomConstraint.constant = -UIScreen.main.bounds.intersection(targetFrame).height
@OscarApeland
OscarApeland / Nibable.md
Last active September 22, 2018 08:42
Swift 4 - Nibable

Introduction

UIKits API for registering UINibs to UICollectionViews looks bad. Just look at this.

sizeCollectionView.register(UINib(nibName: "SearchFilterSizeCell", bundle: nil), forCellWithReuseIdentifier: "sizeCell")

Using them again is even worse;

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sizeCell", for: indexPath) as! SearchFilterSizeCell