制約のなかでContainerViewControllerを
つくる方法

 制約のなかでContainerViewControllerを
つくる方法

ChildViewControllerが依存しあっているような制約のもとでも、汎用のContainerViewControllerを作る方法を詳解します

B6a0c6df530da0ebd7a4f24e6c4643b9?s=128

Mitsuyoshi Yamazaki

September 18, 2018
Tweet

Transcript

  1. 19.

    ΠϯλʔϑΣʔεΛఆٛ͢Δ class ScrollableViewController: UIViewController { var scrollView: UIScrollView } class

    ContainerViewController: UIViewController { var headerViewController: UIViewController // ϔομʔ var viewControllers: [ScrollableViewController] // εΫϩʔϧίϯςϯπ } εΫϩʔϧίϯςϯπ͸ScrollViewΛ΋͍ͬͯΔඞཁ͕͋Δ
  2. 22.

    class ScrollableViewController: UIViewController { var scrollView: UIScrollView } class ContainerViewController:

    UIViewController { var headerViewController: UIViewController // ϔομʔ var viewControllers: [ScrollableViewController] // εΫϩʔϧίϯςϯπ } ChildViewControllerͷ
 ಠཱੑΛ୲อ͢Δ ѻ͑ΔChildViewControllerʹ੍ݶ͕͔͔͍ͬͯΔ
  3. 23.

    protocol Scrollable { var scrollView: UIScrollView! { get } }

    class ContainerViewController: UIViewController { var headerViewController: UIViewController // ϔομʔ var viewControllers: [UIViewController & Scrollable] // εΫϩʔϧίϯςϯπ } ChildViewControllerͷ
 ಠཱੑΛ୲อ͢Δ ඞਢͷ੍໿͸protocolʹ੾Γग़͢
  4. 29.
  5. 30.
  6. 31.

    ScrollView.contentInsetΛར༻͢Δ ࣮૷ tabContentViewController.scrollView.contentInset.top = headerViewController.maximumHeight tabContentViewController.scrollView.observe(\.contentOffset) { (_, change) in

    guard let offset = change.newValue else { return } self.headerHeightConstraint.constant = -(self.tabViewHeight + offset.y) } ContentOffsetͷมԽΛऔಘ͠ɺϔομʔߴ͞Λߋ৽
  7. 36.
  8. 37.