본문 바로가기

swift/snapkit

snapkit(9)

stackview 세로로 같은 사이즈 만들기


1.stackview를 생성한다.

distibution을 fillEqually로 설정하면 stackview의 세로에 맞게 자동적으로 높이를 계산한다.

spacing은 view간의 간격을 정해준다.


stackview에 view를 추가할때ㅑ는 addSubView가 아니고 addArrangedSubview로 해줘야지 view가 정렬된 상태로 들어간다.



import Foundation
import UIKit
import SnapKit


class Stack1ViewController: UIViewController {
  var didSetupConstraints = false


  let stackView1:UIStackView = {
    let stack = UIStackView()
    stack.axis = .vertical
    stack.spacing = 10
    stack.translatesAutoresizingMaskIntoConstraints = false;
    stack.backgroundColor = .red
    stack.alignment = .fill
    stack.distribution = .fillEqually
    return stack
  } ()

  let view1:UIView = {
    let view = UIView()
    view.backgroundColor = .blue
    return view
  }()

  let view2:UIView = {
    let view = UIView()
    view.backgroundColor = .red
    return view
  }()


  override func viewDidLoad() {
    super.viewDidLoad()

    self.initUI()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  static func instance() -> Stack1ViewController {
    let vc = Stack1ViewController()
    return vc
  }
}


//snapkit
extension Stack1ViewController {
  func initUI() {
    self.view.backgroundColor = .white

    self.view.addSubview(stackView1)
    self.stackView1.addArrangedSubview(view1)
    self.stackView1.addArrangedSubview(view2)

    view.setNeedsUpdateConstraints()
  }

  override func updateViewConstraints() {
    if (!didSetupConstraints) {
      stackView1.snp.makeConstraints { make in
        make.top.equalTo(topLayoutGuide.snp.bottom)
        make.bottom.left.right.equalTo(0)
      }

      didSetupConstraints = true
    }

    super.updateViewConstraints()

  }
}



결과화면 




https://github.com/choiks14/SnapkitTest



'swift > snapkit' 카테고리의 다른 글

snapkit(11)  (0) 2017.06.25
snapkit(10)  (0) 2017.06.25
snapkit(9)  (0) 2017.06.25
snapkit(8)  (0) 2017.06.25
snapkit(7)  (0) 2017.06.25
snapkit(6)  (0) 2017.06.25