Skip to content

Instantly share code, notes, and snippets.

@enomoto
Last active July 20, 2022 04:38
Show Gist options
  • Save enomoto/52a6e384d4f8dccb9cca3e275f344905 to your computer and use it in GitHub Desktop.
Save enomoto/52a6e384d4f8dccb9cca3e275f344905 to your computer and use it in GitHub Desktop.
UIStackView initialization idiom
import UIKit
let stackView = UIStackView()
contentView.addSubview(stackView)
stackView.axis = .horizontal // axis's default value is horizontal.
// 以下2行はセット
stackView.isLayoutMarginsRelativeArrangement = true
stackView.directionalLayoutMargins = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
stackView.spacing = 6
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.alignment = .center
stack.distribution = .fillEqually // 等幅
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
stackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12),
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment