LoginSignup
27

More than 5 years have passed since last update.

SwiftでContainerViewを使ってみる

Last updated at Posted at 2017-04-11

Container Viewを使う

StoryBoardにはviewの中にさらにviewを入れ込む事のできるContainer Viewというものが用意されています。

Xcodeの操作になれていないため、自分の備忘用にも使用方法はgifで残しておこうと思います。

まず、Container ViewはObject libraryから探し出すことができます。

a1.gif

Object libraryからContainer Viewを選択し、すでに作っておいたviewの上に載せます。
サイズも適当に合わせてあげましょう。

a3.gif

載せたContainer ViewにViewControllerを設定します。
ViewControllerのコードは以下のようなものを用意します。このViewControllerをContainer Viewに設定するとContainer Viewの部分だけ背景が黄色になります。

ContainerViewController.swift
class ContainerViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //背景色を黄色にする
        self.view.backgroundColor = UIColor.yellow
    }
}

Container ViewへのViewControllerの設定は右側にできている小さいViewのViewControllerを選択しCustom ClassのClassに作成しておいたContainerViewControllerを設定します。

a4.gif

シミュレータで動かしてみましょう。

a5.gif

できた!上下で背景の色が異なっています!

無事Container Viewを使うことができました。

次の投稿では今回行ったContainer Viewの表示部分をコードで実装してみたいと思います。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27