Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to UIStackView

tasanobu
December 10, 2016

Introduction to UIStackView

I made a presentation with this slide at the following meetup.

Tokyo iOS MEETUP: December iOS Meetup
https://www.meetup.com/ja-JP/TokyoiOSMeetup/events/234081164/

tasanobu

December 10, 2016
Tweet

More Decks by tasanobu

Other Decks in Technology

Transcript

  1. Contents • What’s UIStackView? • How UIStackView positions and sizes

    its subviews • How to use the Stack View • Conclusion
  2. UIStackView • A wrapper class of Auto Layout • Available

    on iOS 9 or above • Able to lay out its subviews horizontally or vertically
  3. Main Properties for Layout • UIStackView is configured 
 mainly

    through the following 4 properties • axis • alignment • distribution • spacing
  4. alignment • alignment of the subviews perpendicular to its axis

    • The size of each subview is calculated based on Intrinsic Content Size. (except for .fill) var alignment: UIStackViewAlignment
  5. distribution • distribution is how the stack view lays out

    its subviews along its axis • .fill • .fillEqually • .fillPropotionally • .equalSpacing • .equalCentering var distribution: UIStackViewDistribution
  6. distribution .equalSpacing • Subviews are positioned with the space between

    the views • Subviews are resized based on their intrinsic content size
  7. distribution .equalCentering • Subviews are positioned so that they have

    an equal center- to-center spacing • The spacing are shrinked if the subviews do not fit within the stack view
  8. spacing • Spacing between adjacent edges of subviews • When

    distribution is .equalSpacing and .equalCentering,
 it’s just used as the minimum spacing var spacing: CGFloat distribution: .equalSpacing .fill spacing value is ignored
  9. arrangedSubviews • UIStackView lays out views in this property •

    Needs to add subviews via addArrangedSubviews() • Notes:
 arrangedSubviews is a subset of subviews.
 However, when adding a view with addSubview(),
 the view is not under control of UIStackView var arrangedSubviews: [UIView]
  10. Sample let view = UIView(frame: CGRect(x: 0, y: 0, width:

    200, height: 50)) view.backgroundColor = UIColor.white /// Instantiate StackView and configure it let stackView = UIStackView(frame: .zero) stackView.axis = .horizontal stackView.alignment = .center stackView.distribution = .fill stackView.spacing = 20 stackView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(stackView) /// Setup StackView's constraints to its superview like // view.topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true // view.bottomAnchor.constraint(equalTo: stackView.bottomAnchor).isActive = true /// Add subviews let switchh = UISwitch() switchh.isOn = true switchh.backgroundColor = UIColor.cyan stackView.addArrangedSubview(switchh) let label = UILabel() label.backgroundColor = UIColor.magenta label.text = "label" stackView.addArrangedSubview(label) let button = UIButton(type: .infoDark) button.backgroundColor = UIColor.yellow stackView.addArrangedSubview(button)
  11. Credit • Icons made by Freepik from www.flaticon.com is licensed

    by CC 3.0 BY • Icons made by Tae S Yang from www.flaticon.com is licensed by CC 3.0 BY