Skip to content

Instantly share code, notes, and snippets.

Avatar
😇
What you don't know is what you haven't learned

Khoa onmyway133

😇
What you don't know is what you haven't learned
View GitHub Profile
View a.swift
let marginTop = NSLayoutConstraint(item: box, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 50)
let marginLeft = NSLayoutConstraint(item: box, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 20)
let marginRight = NSLayoutConstraint(item: box, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: -20)
let height = NSLayoutConstraint(item: box, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100)
NSLayoutConstraint.activate([marginTop, marginLeft, marginRight, height])
View a.swift
let views = ["box" : box]
let horizontal = "H:|-20-[box]-20-|"
let vertical = "V:|-50-[box(100)]"
let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: horioptions: [], metrics: nil, views: views)
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: veoptions: [], metrics: nil, views: views)
NSLayoutConstraint.activate([horizontalConstraints, verticalConstraints].flatMap({ $0 }))
View a.swift
public extension Anchor {
/// Find a constraint based on an attribute
func find(_ attribute: Attribute) -> NSLayoutConstraint? {
guard let view = item as? View else {
return nil
}
var constraints = view.superview?.constraints
if attribute == .width || attribute == .height {
constraints?.append(contentsOf: view.constraints)
}
View a.swift
enum To {
case anchor(Anchor)
case size
case none
}
class Pin {
let attribute: Attribute
var constant: CGFloa
init(_ attribute: Attribute, constant: CGFloat = 0) {
self.attribute = attribute
View a.swift
public extension UIView {
func pinCenter(view: UIView) -> [NSLayoutConstraint] {
return [
centerXAnchor.constraint(equalTo: view.centerXAnchor),
centerYAnchor.constraint(equalTo: view.centerYAnchor)
]
}
func pin(size: CGSize) -> [NSLayoutConstraint] {
return [
View a.swift
public extension NSLayoutConstraint {
/// Disable auto resizing mask and activate constraints
///
/// - Parameter constraints: constraints to activate
static func on(_ constraints: [NSLayoutConstraint]) {
constraints.forEach {
($0.firstItem as? UIView)?.translatesAutoresizingMaskIntoConstraints = false
$0.isActive = true
}
View a.swift
class ViewController: UIViewController {
let box = UIView()
override func viewDidLoad() {
super.viewDidLoad()
box.backgroundColor = UIColor.red
view.addSubview(box)
}
View a.swift
import Foundation
public enum Event<T> {
case Next(value: T)
case Failed(error: ErrorType)
// MARK: Initialization
public init(value: T) {
self = .Next(value: value)
}
View a.swift
import Foundation
public final class Signal<T> {
var event: Event<T>?
var callbacks: [Event<T> -> Void] = []
let lockQueue = dispatch_queue_create("lock_queue", DISPATCH_QUEUE_SERIAL)
// MARK: Initialization
public init(event: Event<T>) {
View a.js
import React, {Component} from 'react'
import { StyleSheet, View, Text, SafeAreaView } from 'react-native'
import { facebookService } from '../../library/FacebookService'
import { Avatar } from 'react-native-elements'
export default class ProfilePage extends React.Component {
constructor(props) {
super(props)
this.logout = this.logout.bind(this)
You can’t perform that action at this time.