UILayoutGuideを活用する/love_swift36

3e66b3ccfa617b78c59d9dec865aa29f?s=47 noppefoxwolf
November 09, 2018

 UILayoutGuideを活用する/love_swift36

3e66b3ccfa617b78c59d9dec865aa29f?s=128

noppefoxwolf

November 09, 2018
Tweet

Transcript

  1. 6*-BZPVU(VJEFΛ׆༻͢Δ 4XJGUѪ޷ձWPM 

  2. OPQQF ! σΟʔɾΤψɾΤʔ " ϥΠϒ഑৴J04ΞϓϦ # ͖ͭͶ͔Θ͍͍ 

  3. 6*-BZPVU(VJEF w "VUPMBZPVUͷػೳΛ࣋ͬͨදࣔ͞Εͳ͍ϏϡʔͷΑ ͏ͳ΋ͷ w "ODIPS͔࣋ͨ͠ͳ͍γϯϓϧͳΫϥε w J1IPOF9Ͱ͸ϘλϯɾϊονΛআ͘ηʔϑΤϦΞΛ -BZPVU(VJEFͰ࣮૷͍ͯ͠Δ 

  4. 6*-BZPVU(VJEF ྫɿWJFXTBGF"SFB-BZPVU(VJEF IUUQTRJJUBDPNHFOUMFKLPWJUFNTBEC 

  5. dJ04 w എܠ৭ΛDMFBSʹͨ͠ϏϡʔΛ഑ஔͯ͠ɺͦΕʹର͠ ੍ͯ໿Λ͔͚͍ͯͨɻ 

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

    ϨΠΞ΢τʹෆཁͳػೳ ͕ଘࡏ ࠷௿ݶ ϏϡʔώΤϥϧΩͷߋ৽ ͞ΕΔ ͞Εͳ͍ 
  7. ࣗ࡞ͷ6*-BZPVU(VJEFΛੜ΍͢ 

  8. ࣗ࡞ͷ6*-BZPVU(VJEFΛੜ΍͢ w TBGB"SFB-BZPVU(VJEFͳͲσϑΥϧτ࣮૷΋͋Γ w 6*-BZPVU(VJEF͸ࣗ෼Ͱ௥Ճ͢Δ͜ͱ΋Մೳ 

  9. GSFTI04,FZCPBSE-BZPVU(VJEF ΩʔϘʔυͷҐஔʹ-BZPVU(VJEFΛੜ΍͢&YUFOTJPO 

  10. 

  11. OPQQFGPYXPMG5FYU$POUBJOFS-BZPVU(VJEF UFYU$POUBJOFSʹ-BZPVU(VJEFΛੜ΍͢&YUFOTJPO 

  12. OPQQFGPYXPMG5FYU$POUBJOFS-BZPVU(VJEF 

  13. ࣗ࡞ͷ6*-BZPVU(VJEFΛੜ΍͢ϝϦοτ w ෳࡶͳ੍໿ͷมԽΛ7$͔ΒӅṭग़དྷΔ w .BOVBM-BZPVU͕ͪ͠ͳՕॴΛݮΒͤΔ 

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

    object: nil) ~~ NotificationCenter.default.removeObserver(self) ~~ viewBottomConstraint.constant = height view.layoutIfNeeded() 
  15. ෳࡶͳ੍໿ͷมԽΛӅṭग़དྷΔ button.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor).isActive = true 

  16. ੩తɾ൒ಈతͳϨΠΞ΢τ ˠ4UPSZCPBSE 9*# ಈతɾϓϩάϥϚϒϧͳϨΠΞ΢τ ˠ6*-BZPVU(VJEF 

  17. -BZPVU(VJEF։ൃνϡʔτϦΞϧ 

  18. ྫɿ/PUDI-BZPVU(VJEF w /PUDIͷ৔ॴʹ-BZPVU(VJEFΛ഑ஔ͢Δ w ͭOPQQFGPYXPMG/PUDI-BZPVU(VJEF 

  19. 

  20. ྲྀΕ w 6*-BZPVU(VJEFͷαϒΫϥεΛ࡞Δ w 6*8JOEPXʹࠇຐज़ͰMBZPVU(VJEFΛੜ΍͢ w ճసʹΑ੍ͬͯ໿Λߋ৽͢Δ 

  21. 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͘Β͍ 
  22. 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Λੜ΍͢ࠇຐज़Λ࢖͏ͱศར 
  23. ಈ࡞ͷ֬ೝ 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ͷԼʹ഑ஔ͢Δ੍໿Λͭ ͚Δ 
  24. 

  25. 

  26. ճసʹΑ੍ͬͯ໿Λߋ৽͢Δ NotificationCenter.default.addObserver(self, selector: #selector(didRotate), name: .UIDeviceOrientationDidChange, object: nil) ~~ deinit

    { NotificationCenter.default.removeObserver(self) } 
  27. ճసʹΑ੍ͬͯ໿Λߋ৽͢Δ private func update() { guard let window = owningView

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

  29. 

  30. 6*-BZPVU(VJEFͰग़དྷͳ͍ࣄ w ୹ܗྖҬͰͳ͍΋ͷ͸ੜ΍ͤͳ͍ w ճసʹऑ͍ w %FCVH7JFX)JFSBMDZͰݟ͑ͳ͍ w ࣗ࡞-BZPVU(VJEF͸4UPSZCPBSE͔Β੍໿෇͚Ͱ͖ͳ ͍

    
  31. ·ͱΊ w -BZPVU(VJEF͸ࣗ࡞Ͱ͖Δ w ૿΍ͯ͠΋ϨϯμϦϯάίετ͸ͳ͍ w ෳࡶͳಈతॲཧ΋7$͔Β੾Γ཭ͤΔ 

  32. ͋Γ͕ͱ͏͍͟͝·ͨ͠