Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Farewell SnapKit

kameike
July 26, 2018

Farewell SnapKit

ありがとう!!!!さようなら!(?)

kameike

July 26, 2018
Tweet

More Decks by kameike

Other Decks in Programming

Transcript

  1. let constraints = NSLayoutConstraint( item: subview, attribute: .left, relatedBy: .equal,

    toItem: view, attribute: .left, multiplier: 1.0, constant: 0.0) 普通に作る 10
  2. 10

  3. 10

  4. // SnapKit subview.snp.makeConstraints { constraintMaker in constraintMaker.top.equalTo(_ other: ConstraintRelatableTarget) }

    // ConstraintRelatableTargetとして使える型 // Int UInt Float Double CGFloat CGSize CGPoint // UIEdgeInsets, UIView SnapKit 10
  5. // SnapKit let offset = UIEdgeInsets(top: 10, left: 10, bottom:

    10, right: 10) make.edges.equalTo(view).offset(offset) Edgeがほしい! 10
  6. extension UIView { func edges(equalTo view: UIView, offset: UIEdgeInsets =

    UIEdgeInsets()) { NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: view.topAnchor, constant: offset.top), bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -offset.bottom), leftAnchor.constraint(equalTo: view.leftAnchor, constant: offset.left), rightAnchor.constraint(equalTo: view.rightAnchor, constant: -offset.right) ]) } } extensionでなんとかなる!10行ぐらい! 10
  7. // SnapKit view.edges.equalTo(view).offset(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))

    // Anchor+Extension view.edges(equalTo: view, offset: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)) なんとかなった...! 10
  8. // leading, trailing extension UIView { func edges(equalTo view: UIView,

    offset: NSDirectionalEdgeInset) { NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: view.topAnchor, constant: offset.top), bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -offset.bottom), leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: offset.leadingAnchor), trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -offset.trailingAnchor) ]) } } 余談 leading trailingを使いたいなら 10
  9. let offset = 3 * 5 // SnapKit だと大丈夫だけど make.top.equalTo(view).offset(offset)

    // Anchor だとコンパイルエラー subview.topAnchor.constraint(equalTo: view.topAnchor, constant: offset) ConstraintXXXTarget的なものを CGFloatにする必要がある 10
  10. @available(iOS 6.0, *) open func addConstraint(_ constraint: NSLayoutConstraint) // This

    method will be deprecated in a future release and should be avoided. // Instead, set NSLayoutConstraint's active property to YES. 10 addConstraintは 使わない方がいい
  11. let constraint = view.bottomAnchor.constraint(equalTo: bottomAnchor) constraint.isActive = true // もしくは

    NSLayoutConstraint.activate([constraint]) // でconstraintを有効に...! こういう感じで 10