Skip to content

Instantly share code, notes, and snippets.

View nekonora's full-sized avatar
🎸
Making stuff using other stuff.

Filippo Zaffoni nekonora

🎸
Making stuff using other stuff.
View GitHub Profile
@nekonora
nekonora / addView.swift
Last active March 2, 2019 13:26
UIView quick add programatically
addedView = UIView()
addSubview(addedView)
addedView.translatesAutoresizingMaskIntoConstraints = false
addConstraints([
NSLayoutConstraint(item: addedView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: addedView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: addedView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: addedView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1.0, constant: 0)
let attributedText = NSMutableAttributedString(
string: "First string",
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .semibold)])
attributedText.append(NSAttributedString(
string: "Bold string",
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .heavy)]))
attributedText.append(NSAttributedString(
string: "Last string.",
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String
import UIKit
class ModularView: UIView {
// MARK: - Meta properties
let xibName = "ModularView"
import UIKit
public extension UIDevice {
static let modelName: String = {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
@nekonora
nekonora / AnimationSyncToKeyboard.swift
Last active July 14, 2022 04:46
Sync animation to keyboard appearing
@objc func keyboardWillShow(_ notification: NSNotification) {
let keyboardAnimationDetail = notification.userInfo
let animationCurve: Int = {
if let keyboardAnimationCurve = keyboardAnimationDetail?[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int {
let curve: Int? = UIView.AnimationCurve(rawValue: keyboardAnimationCurve)?.rawValue
return curve ?? 0
} else {
return 0
}
extension MyViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableView { // Table view for example
var scrollY = scrollView.contentOffset.y + scrollView.contentInset.top + 40
if scrollY < 0 { scrollY = 0 }
constraintToChange.constant = -scrollY
if 0...10 ~= scrollY { viewToHide.alpha = 1 - (scrollY.rounded() / 10.0) }
@nekonora
nekonora / Storage.swift
Created April 24, 2020 13:34
A useful little storage class for anything conforming to Codable, using UserDefaults and property wrappers
//
// Storage.swift
//
import Foundation
class Storage {
// MARK: - Keys
import UIKit
extension UIView {
func setConstraint(_ constraintBlock: ((UIView) -> Void)) {
translatesAutoresizingMaskIntoConstraints = false
constraintBlock(self)
layoutIfNeeded()
}
import Foundation
import UIKit
class ModalScreenVC: UIViewController {
// MARK: - Outlets
/// A subview filling this controller's view with desired top margin
@IBOutlet private var contentView: UIView!