Last active
July 20, 2022 04:38
-
-
Save enomoto/52a6e384d4f8dccb9cca3e275f344905 to your computer and use it in GitHub Desktop.
UIStackView initialization idiom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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