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

まだ Storyboard で消耗してるの?

Elvis Shi
November 29, 2016

まだ Storyboard で消耗してるの?

Elvis Shi

November 29, 2016
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

    ktanaka117 ࢯʹʮͦͷՖʯʹϋϚΒͤͨ ݩڟˡ!!!
  2. 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 }
  3. 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) }
  4. Tips on layoutSubviews() transform ϓϩύςΟʔʹมߋ͕͋ΔڪΕͷ͋ΔϏϡʔ͸ frame Ͱ͸ ͳ͘ bounds ͱ

    center Λઃఆ͢Δ frame ϓϩύςΟʔ͸ͦ΋ͦ΋ bounds ͱ center ʹ transform Λ ద༻ͨ͠৔߹ͷදݱʹ͗͢ͳ͍ ͦͷͨΊ .identity Ҏ֎ͷ transform ϓϩύςΟʔΛ࣋ͭ UIView ʹ௚઀ frame Λઃఆ͢Δͱද͕͓͔ࣔ͘͠ͳΓ·͢