Skip to content

Instantly share code, notes, and snippets.

@iSapozhnik
Created April 23, 2020 19:30
Show Gist options
  • Save iSapozhnik/10e59bb9c35ca3a08cc921e1c7bc7022 to your computer and use it in GitHub Desktop.
Save iSapozhnik/10e59bb9c35ca3a08cc921e1c7bc7022 to your computer and use it in GitHub Desktop.
UIStackView with custom spacing after arranged subview
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