LoginSignup
5
2

More than 5 years have passed since last update.

UIStackViewとAutoLayoutとUIViewをコードから使ってみるサンプル

Last updated at Posted at 2018-06-24

AutoLayoutを使って、UIStackViewを画面中央に配置し、
その中にUIViewとUISwitchを入れてみるサンプル。

UIStackViewは縦と横で必ず何かしらのAutoLayoutを指定しないといけないようでした。


        let stack:UIStackView = UIStackView()
        stack.backgroundColor = UIColor.cyan
        view.addSubview(stack)
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        stack.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true


        let myView = UIView()
        myView.backgroundColor = UIColor.brown
        myView.translatesAutoresizingMaskIntoConstraints = false
        myView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        myView.heightAnchor.constraint(equalToConstant: 100).isActive = true

        stack.addArrangedSubview(myView)
        stack.addArrangedSubview(UISwitch())
        stack.addArrangedSubview(UISwitch())
        stack.addArrangedSubview(UISwitch())

IMG_0009.png

5
2
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
5
2