1

I'm programmatically building a vertical UIStackView to show 3 short UILabel's:

import UIKit
import PlaygroundSupport

let box = UIView(frame: CGRect(origin: CGPoint(x:0, y:0), size: CGSize(width: 100, height: 100)))
box.backgroundColor = .white

let labels: [UILabel] = (1...3).map{ index in
   let label = UILabel()
   label.text = "item \(index)"
   return label
}

let sv = UIStackView(arrangedSubviews: labels)
sv.axis = .vertical
sv.alignment = .center
sv.translatesAutoresizingMaskIntoConstraints = false

box.addSubview(sv)

sv.centerXAnchor.constraint(equalTo: box.centerXAnchor).isActive = true
sv.centerYAnchor.constraint(equalTo: box.centerYAnchor).isActive = true

// performing this font change on a single item collapses the stack view
if let label = sv.subviews[1] as? UILabel {
    label.font = UIFont.italicSystemFont(ofSize: UIFont.labelFontSize)
}

PlaygroundPage.current.liveView = box

Performing font change of a single label from the stack view as shown above makes the labels collapse onto each other turning into unreadable blob of letters:

Instead of

enter image description here

I get

enter image description here

Am I missing some basic required constraint?

I should mention I'm using Xcode 10.2.1 Playground to develop this view.

Srg
  • 510
  • 4
  • 13
  • I'm guessing this is a Playground bug... If I skip the italic setting, but add a button to change the font after the view has been rendered, no problem. Also, can't replicate it in an app... – DonMag Jul 10 '19 at 19:28
  • @DonMag it probably is.. Thanks for checking it out! – Srg Jul 10 '19 at 20:36

0 Answers0