6

I have a UIView with a custom draw(_ rect: CGRect) method, which works.

However, when I place it inside a UIStackView, its draw method is not called. UILabel or UIImage inside the same stack view display normally.

In Interface Builder, it is drawn even inside the stack view, but when running on the device, it's not.

Dieter Baron
  • 101
  • 3
  • Same issue here, somehow it's not mentioned anywhere else. – Schemetrical Feb 05 '19 at 03:26
  • I'm having the same issue. I have some custom views drawing with code from PaintCode. They draw perfectly outside of a UIStackView, but when inside a UIStackView the draw() method is never even called (although they DO show up in the storyboard). Did you find any solution? – Kenny Wyland Oct 23 '19 at 21:26

3 Answers3

0

draw(_ rect: CGRect) doesn't call in addArrangedSubview of stack. Instead you can put your code into layoutSubviews.

Ruli
  • 2,592
  • 12
  • 30
  • 40
0

Solved it by adding a wrapper view first and after adding it to the UIStackView, i added my Custom-Drawing view into the wrapper view. (This is written in C# but I'm pretty sure it'll help anyway)

UIView wrapperView = new UIView(f);

CanvasStackView.AddArrangedSubview(wrapperView);

CGRect f = new CGRect(CGPoint.Empty,new CGSize(CanvasStackView.Frame.Size.Width, height));
MyCustomView b = new MyCustomView(f);

wrapperView.AddSubview(b);
Chris
  • 36
  • 4
0

I've had the same issue. Check your view's constraints. I've added leading/trailing space and height to resolve this.