LoginSignup
4
1

More than 3 years have passed since last update.

Xibを使ったView遷移

Posted at

xibを使ったVeiw遷移のやり方をいつも忘れるので備忘録としてここに記す。

xib作成

xibとSwiftファイルを作成後、SwiftファイルにUIクラスを継承させないとxibファイルと結びつけることはできない
スクリーンショット 2019-11-10 16.45.44.png
スクリーンショット 2019-11-10 16.44.48.png

ViewController側

VeiwController側でxibを呼び出し、呼び出したコントローラーのviewに設定する


let view1: UIView = UINib(nibName: "CustomeView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
        self.view.addSubview(view1)

全体図


    @IBOutlet weak var buttonaction: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }


    @IBAction func buttonAction(_ sender: Any) {
        let view: UIView = UINib(nibName: "CustomeView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
        self.view.addSubview(view)
    }

4
1
0

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
4
1