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

UILayoutGuideを活用する/love_swift36

noppefoxwolf
November 09, 2018

 UILayoutGuideを活用する/love_swift36

noppefoxwolf

November 09, 2018
Tweet

More Decks by noppefoxwolf

Other Decks in Programming

Transcript

  1. 6*-BZPVU(VJEF 6*7JFX 6*-BZPVU(VJEF "VUPMBZPVU ˓ ˓ ϨϯμϦϯάίετ º ˓ ػೳͷγϯϓϧ͞

    ϨΠΞ΢τʹෆཁͳػೳ ͕ଘࡏ ࠷௿ݶ ϏϡʔώΤϥϧΩͷߋ৽ ͞ΕΔ ͞Εͳ͍ 
  2. 

  3. ෳࡶͳ੍໿ͷมԽΛӅṭग़དྷΔ let nc = NotificationCenter.default nc.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), name: UIResponder.keyboardWillChangeFrameNotification,

    object: nil) ~~ NotificationCenter.default.removeObserver(self) ~~ viewBottomConstraint.constant = height view.layoutIfNeeded() 
  4. 

  5. 6*-BZPVU(VJEFͷαϒΫϥεΛ࡞Δ public class NotchLayoutGuide: UILayoutGuide { internal func setup() {

    guard let window = owningView else { return } NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: window.topAnchor), centerXAnchor.constraint(equalTo: window.centerXAnchor), widthAnchor.constraint(equalToConstant: 230), heightAnchor.constraint(equalToConstant: 30), ]) } } ˞/PUDIͷαΠζ͸Y͘Β͍ 
  6. 6*8JOEPXʹMBZPVU(VJEFΛੜ΍͢ public extension UIWindow { public var notchLayoutGuide: NotchLayoutGuide {

    if let obj = objc_getAssociatedObject(self, &AssociatedKeys.notchLayoutGuide) as? NotchLayoutGuide { return obj } let new = NotchLayoutGuide() addLayoutGuide(new) new.setup() objc_setAssociatedObject(self, &AssociatedKeys.notchLayoutGuide, new as Any, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) return new } } FYUFOTJPOʹTUPSFEQSPQFSUZΛੜ΍͢ࠇຐज़Λ࢖͏ͱศར 
  7. ಈ࡞ͷ֬ೝ let button = UIButton(frame: .zero) button.translatesAutoresizingMaskIntoConstraints = false view.addSubview(button)

    NSLayoutConstraint.activate([ button.topAnchor.constraint(equalTo: window.notchLayoutGuide.bottomAnchor), button.centerXAnchor.constraint(equalTo: window.notchLayoutGuide.centerXAnchor), button.widthAnchor.constraint(equalToConstant: 100), button.heightAnchor.constraint(equalToConstant: 44), ]) ϘλϯΛ/PUDI-BZPVU(VJEFͷԼʹ഑ஔ͢Δ੍໿Λͭ ͚Δ 
  8. 

  9. 

  10. ճసʹΑ੍ͬͯ໿Λߋ৽͢Δ private func update() { guard let window = owningView

    else { return } allConstraint.isActive = false //extension switch UIDevice.current.orientation { case .portrait: ... case .landscapeLeft: ... ..... 
  11. 

  12.