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

とっさのAutoLayout - Rewind autolayout

とっさのAutoLayout - Rewind autolayout

札幌iPhoneアプリ開発懇談会2024年2月勉強会資料

iOSアプリ開発でUIレイアウトを調整するためにAppleが提供しているのがAutoLayoutです。SwiftUIの登場で注目度は低くなり、かつUIのレイアウトを扱う頻度で言うと多くはなく、いざAutoLayoutの使用を迫られた時に忘れがちな使用パターンを説明します。

notoroid

April 12, 2024
Tweet

More Decks by notoroid

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ w J1IPOFΞϓϦ։ൃऀ 9!OPUPSPJE  w ΞϓϦ։ൃऀྺ ʙ  w

    ΞϓϦ࡞࢛ͬͯ൒ੈل ೥౸ୡ  w झຯ͸ήʔϜ ൓ࣹਆܦ͕ਰ͑ͱ໨ͷർΕ͕ൈ͚ͳ͍
  2. "VUP-BZPVU֓આ "VUP4J[JOH ʙ w J1IPOF։ൃ౰ॳ  ͷը໘ϨΠΞ΢τ w 04ᶠ NBD04

    ͔Β։ൃࢿ࢈ΛҾ͖ܧ͗ɺ(6*࡞੒πʔϧ *OUFSGBDF #VJMEFS Ͱ(6*ϨΠΞ΢τΛ࡞੒͢Δ͜ͱ͕Ͱ͖ͨɻ(6*Ϧιʔε͚ͩͰͳ ͘ϓϩάϥϛϯάίʔυͰϨΠΞ΢τΛࢦఆ͢Δ͜ͱ͕Ͱ͖ͨɻ
  3. "VUP-BZPVU֓આ "VUP-BZPVUͷొ৔ ʙ w ϨΠΞ΢τΛೋ࣍ํఔࣜͰࢦఆ͢Δ w 6*σβΠϯͰ΋ϓϩάϥϜͰ΋هड़Ͱ͖Δͷ͸ೋ࣍ํఔࣜ w σβΠϯஈ֊Ͱ੔߹ੑͷνΣοΫ͕ߦΘΕΔ w

    ϨΠΞ΢τ͸7JFXͷ֊૚ߏ଄ಉ֊૚ͷ7JFXҎ֎͔Β΋఻೻͞ΕΔ w ϨΠΞ΢τࢦఆ͸ݸผʹ0O0 ff Մೳ w ԾͷϨΠΞ΢τ΍ॎԣൺͷมԽͰͷ੾Γସ͑
  4. ΩʔϘʔυपΓͷߋ৽ w 6*,FZCPBSE-BZPVU(VJEF ΩʔϘʔυ ͷදࣔඇදࣔ޲͚ΨΠυ ʹϓϩύςΟ ௥Ճ w VTFT#PUUPN4BGF"SFB w

    ΩʔϘʔυԼ෦ϨΠΞ΢τҐஔͷઃ ఆ w LFZCPBSE%JTNJTT1BEEJOH w ΩʔϘʔυͷσΟεϛε։࢝Ґஔͷ ௐ੔ 88%$Ξοϓσʔτ
  5. "VUPMBZPVUTIPXDBTF ϢʔςΟϦςΟίʔυͷ঺հᶃ import Foundation // Ϋϥε໊ͷαϙʔτ protocol WithClassName: NSObject {

    } extension WithClassName { static var className: String { NSStringFromClass(Self.self).components(separatedBy: ".").last ?? "" } } Ϋϥε໊Λࣝผ༻*%΍9JCϑΝΠϧ໊ʹ࢖͏ͨΊ
  6. "VUPMBZPVUTIPXDBTF ϢʔςΟϦςΟίʔυͷ঺հᶄ import UIKit // XibϑΝΠϧ͔ΒViewΛΠϯελϯεԽ(generic࢖༻) extension UINib { func

    instantiate<T: UIView>() -> T { instantiate(withOwner: nil, options: nil)[0] as? T ?? T() } } 9JCϑΝΠϧ͔Β7JFXΛϩʔυ͢Δ
  7. "VUPMBZPVUTIPXDBTF ϢʔςΟϦςΟίʔυͷ঺հᶅ import UIKit // [Ϋϥε໊].xib͔ΒcontentViewΛϩʔυ͢Δ protocol WithNibLoad: UIView, WithClassName

    { var contentView: UIView! { get } } extension WithNibLoad { func loadContentView(){ let nib = UINib(nibName: Self.className, bundle: nil) nib.instantiate(withOwner: self, options: nil) addSubview(contentView) contentView.translatesAutoresizingMaskIntoConstraints = false contentView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true contentView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true contentView.topAnchor.constraint(equalTo: topAnchor).isActive = true contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true } }
  8. "VUPMBZPVUTIPXDBTF 6*5BCMF7JFXͷ)FBEFS7JFXʹ"VUPMBZPVU͸࢖͑Δ w ࢖͑Δ w 6*5BCMF7JFXͷ)FBEFS7JFX͸UBCMF7*FXʹ૊Έࠐ·ΕΔલ·Ͱ"VUP-BZPVU Ͱ෯ߴ͞Λଞͷ7JFX͔ΒಘΔ͜ͱ͕Ͱ͖ͳ͍ ֊૚ΤϥʔͱͳΔ  w

    )FBEFS7JFXʹ͢Δ7JFXࣗମ͸"VUP4J[JOHΛ༗ޮԽ͢Δ w 6*7JFXTZTUFN-BZPVU4J[F'JUUJOH Λ࢖͏ͱ)FBEFS7JFXͷ"VUPMBZPVU৘ ใʹج͍ͮͯαΠζΛࢉग़Մೳ w 6*5BCMF7JFX)FBEFS'PPUFS7JFXΛ࢖͏ํ͕ਖ਼͍͠
  9. "VUPMBZPVUTIPXDBTF 6*5BCMF7JFXͷ)FBEFS7JFXʹ"VUPMBZPVU͸࢖͑Δ func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) ->

    UIView? { let patternBView = PatternBView() patternBView.label.text = """ ϔομʔʹ௕จͷλΠτϧΛ෇Ճ͠·͢ɻ վߦ΋ڐՄ͠·͢ɻ """ patternBView.translatesAutoresizingMaskIntoConstraints = true patternBView.autoresizingMask = [.flexibleWidth, .flexibleHeight] patternBView.frame.size = patternBView.systemLayoutSizeFitting( CGSize(width: tableView.frame.width, height: CGFloat.greatestFiniteMagnitude), withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel ) return patternBView }
  10. ࢀߟ w 9*#͔Β7JFXΛੜ੒͢Δͭͷ࣮૷ύλʔϯ w IUUQTRJJUBDPNNBSUZTV[VLJJUFNT DFDEDCGDC w 6*5BCMF7JFXͰΧελϚΠζϔομʔΛ࢖͏ ͘·T5FDIܥ#MPH w

    IUUQTLVNBTLVOIBUFOBCMPHDPN FOUSZ w J04ͰͷΩʔϘʔυ࢓༷มߋ৽ػೳϢ χϑΝ։ൃऀϒϩά w IUUQTUFDIVOJGBFDPNFOUSZ JPT@LFZCPBSE w OPUPSPJE5BCMF7JFX5FTUCFE 5BCMF7JFX5FTUCFEGPSJ04BQQ w IUUQTHJUIVCDPNOPUPSPJE 5BCMF7JFX5FTUCFE w JQIPOF*TJUQPTTJCMFUPVTF"VUP-BZPVU XJUI6*5BCMF7JFXTUBCMF)FBEFS7JFX  4UBDL0WFS fl PX w IUUQTTUBDLPWFS fl PXDPNRVFTUJPOT JTJUQPTTJCMFUPVTF BVUPMBZPVUXJUIVJUBCMFWJFXT UBCMFIFBEFSWJFX