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

まだ Storyboard で消耗してるの?

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
November 29, 2016

まだ Storyboard で消耗してるの?

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

November 29, 2016
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. ·ͩ Storyboard Ͱফ໣ͯ͠Δͷʁ ϏδϡΞϧϦϓϦθϯςʔγϣϯ ᴷᴷRe:θϩ͔Β࢝ΊΔࢹ֮දݱ ʢ࣮ફฤʣ

  2. Who am I? MAGES. Inc. Game Div. iOS Developer @loveeʢ੕໺ܙྤʣ

    ktanaka117 ࢯʹʮͦͷՖʯʹϋϚΒͤͨ ݩڟˡ!!!
  3. None
  4. Storyboard ͷࡑ ಈతʹมΘΔϨΠΞ΢τʹϓϨϏϡʔͮ͠Β͍ ࢓༷มߋʹରԠ͠ʹ͍͘ ιʔείʔυͱͷϦϯΫ͕ʮ!ʯ ιʔείʔυͱͷؔ܎͕Θ͔ΓͮΒ͍ ViewController ʹ࢓ࣄԡ͠෇͚͗͢

  5. Auto Layout ͱ͔͍͏ΰϛ ෳࡶͳϨΠΞ΢τʹରԠͰ͖ͳ͍ ੍໿͸શͯಉҰώΤϥϧΩʔ ʮ=ʯ͸ʮ୅ೖʯͰ͸ͳ͘ʮ౳߸ʯ Ϋιଟ͍ɺ୳ͮ͠Β͍ɺϝϯςੑ௿͍ ϒϥοΫϘοΫε

  6. ࣮ફฤ

  7. ViewController ͷ࢓ࣄ // ࣗલͷϏϡʔΛΛ VC ͷϏϡʔʹઃఆ͢Δ
 override func loadView() {

    let view = View(frame: UIScreen.main.bounds) self.view = view } // ͓ͳ͡Έͷ viewDidLoad Ͱͷৄࡉઃఆ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = .white }
  8. View ͷ࢓ࣄ // ඞཁͳαϒϏϡʔͳͲͷએݴ΍ઃఆ lazy var label: UILabel = {

    let label = UILabel() label.text = "Hello, World" return label }() // αϒϏϡʔΛ௥Ճ͢Δ override func didMoveToSuperview() { super.didMoveToSuperview() self.addSubview(self.label) } // αϒϏϡʔΛϨΠΞ΢τ͢Δ override func layoutSubviews() { super.layoutSubviews() self.label.sizeToFit() self.label.center = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2) }
  9. Tips on layoutSubviews() ࣗ෼Ͱࣗ෼ͷαΠζΛઃఆ͠ͳ͍ ϝϯςੑͷ޲্ ඳըαΠΫϧͷಛੑ αΠζ͕ಈతʹมΘΔϏϡʔ͸ sizeThatFits(_:) ϝιουͷΦʔ όʔϥΠυͳͲͰ਌Ϗϡʔʹઃఆͯ͠΋Β͏

  10. Tips on layoutSubviews() transform ϓϩύςΟʔʹมߋ͕͋ΔڪΕͷ͋ΔϏϡʔ͸ frame Ͱ͸ ͳ͘ bounds ͱ

    center Λઃఆ͢Δ frame ϓϩύςΟʔ͸ͦ΋ͦ΋ bounds ͱ center ʹ transform Λ ద༻ͨ͠৔߹ͷදݱʹ͗͢ͳ͍ ͦͷͨΊ .identity Ҏ֎ͷ transform ϓϩύςΟʔΛ࣋ͭ UIView ʹ௚઀ frame Λઃఆ͢Δͱද͕͓͔ࣔ͘͠ͳΓ·͢
  11. Tips on layoutSubviews() ͳΔ΂͘γϯϓϧͰܭࢉྔΛ͔͚͗͢ͳ͍Α͏ʹ৺͕͚Δ ϑϨʔϜมߋ΍ setNeedsLayout() ͕ݺ͹Εͨ࣌ʹඞͣݺ͹ΕΔ ࣗ෼͚ͩͰͳࣗ͘෼ͷࢠϏϡʔͷ layoutSubviews() ΋ݺ͹ΕΔ

    Ϗϡʔ͕ଟ͍΄Ͳܭࢉྔ͕૿͑Δ
  12. ָ͍͠ϨΠΞ΢τϥΠϑΛʂ

  13. Thank you