Created
April 23, 2020 19:30
-
-
Save iSapozhnik/10e59bb9c35ca3a08cc921e1c7bc7022 to your computer and use it in GitHub Desktop.
UIStackView with custom spacing after arranged subview
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
extension UIStackView { | |
func setSpacing(_ spacing: CGFloat, after arrangedSubview: UIView) { | |
guard let index = arrangedSubviews.firstIndex(of: arrangedSubview) else { return } | |
let spaceView = UIView() | |
spaceView.translatesAutoresizingMaskIntoConstraints = false | |
switch axis { | |
case .horizontal: | |
spaceView.widthAnchor.constraint(equalToConstant: spacing).isActive = true | |
case .vertical: | |
spaceView.heightAnchor.constraint(equalToConstant: spacing).isActive = true | |
@unknown default: | |
fatalError() | |
} | |
insertArrangedSubview(spaceView, at: index + 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment