Skip to content

Instantly share code, notes, and snippets.

@morishin
Last active March 22, 2024 00:25
Show Gist options
  • Save morishin/639f5ff12c544eac9d7c64acbca54270 to your computer and use it in GitHub Desktop.
Save morishin/639f5ff12c544eac9d7c64acbca54270 to your computer and use it in GitHub Desktop.
Align to Left the contents of UIStackView
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = .white
let stack = UIStackView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
stack.axis = .horizontal
stack.alignment = .center
stack.distribution = .fill
stack.spacing = 8
let label = UILabel()
label.text = "ラベル"
label.backgroundColor = .blue
let button = UIButton(type: .roundedRect)
button.setTitle("ボタン", for: .normal)
button.backgroundColor = .green
let spacerView = UIView()
spacerView.setContentHuggingPriority(.defaultLow, for: .horizontal)
stack.addArrangedSubview(label)
stack.addArrangedSubview(button)
stack.addArrangedSubview(spacerView)
view.addSubview(stack)
PlaygroundPage.current.liveView = view
@atereshkov
Copy link

That is incredibly helpful! Thanks for the idea with dummy spacer view :)

@morishin
Copy link
Author

;)

@DevinThompson
Copy link

So helpful! Thanks!

@josuesilva-hotmart
Copy link

Awesome!

@moquiutijunio
Copy link

Thanks

@guzhavin
Copy link

Good work. Thank you!

@yojkim
Copy link

yojkim commented Nov 13, 2019

Awesome! Thanks 👍

@chawda
Copy link

chawda commented Jan 11, 2021

Thanks. Adding spacer view in last is the key.

@MohauMDiscovery
Copy link

Thank you! 🙂

@naijab
Copy link

naijab commented Feb 22, 2021

Thanks, awesome

@torburg
Copy link

torburg commented Jun 4, 2021

thanks! but could you explain, how it works?

@Angkur03
Copy link

wow, you save my day man <3

@CyberMew
Copy link

CyberMew commented Oct 8, 2021

This does not work for me, for some reason it still is divided into 3 columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment