The problem is, that we have view hierarchy with multiple levels of views that uses autoSizeThatFits function to calculate its size and the resulting size is wrong.
Here is the example of view hierarchy and code to reproduce this bug.
Hierarchy

Code
classGreenView:UIView{varchild=YellowView()overridefunc sizeThatFits(_ size:CGSize)->CGSize{letsize=autoSizeThatFits(size, layoutClosure: layout)return size
}overridefunc layoutSubviews(){
super.layoutSubviews()layout()}func layout(){
child.pin.left().top().bottom().right().margin(10).sizeToFit(.width)}overrideinit(frame:CGRect){
super.init(frame: frame)setupUI()}requiredinit?(coder:NSCoder){fatalError("init(coder:) has not been implemented")}privatefunc setupUI(){
backgroundColor =.green
addSubview(child)}}classYellowView:UIView{varchild=RedView()overridefunc sizeThatFits(_ size:CGSize)->CGSize{letsize=autoSizeThatFits(size, layoutClosure: layout)return size
}overridefunc layoutSubviews(){
super.layoutSubviews()layout()}func layout(){
child.pin.left().top().bottom().right().margin(10).sizeToFit(.width)}overrideinit(frame:CGRect){
super.init(frame: frame)setupUI()}requiredinit?(coder:NSCoder){fatalError("init(coder:) has not been implemented")}privatefunc setupUI(){
backgroundColor =.yellow
addSubview(child)}}classRedView:UIView{letlabel=UILabel()overridefunc sizeThatFits(_ size:CGSize)->CGSize{letsize=autoSizeThatFits(size, layoutClosure: layout)return size
}overridefunc layoutSubviews(){
super.layoutSubviews()layout()}func layout(){
label.pin.all().sizeToFit(.width)}overrideinit(frame:CGRect){
super.init(frame: frame)setupUI()}requiredinit?(coder:NSCoder){fatalError("init(coder:) has not been implemented")}privatefunc setupUI(){
backgroundColor =.red
addSubview(label)
label.text ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dolor est, interdum vitae congue bibendum, semper quis quam. Fusce commodo varius pretium. Pellentesque feugiat lacus vitae sapien faucibus, non rutrum purus fringilla."
label.numberOfLines =0}}Just add GreenView to your example view and layout it like
greenView.pin.top().start().end().sizeToFit(.width)
The sizes of these views would be:
| View | sizeThatFits(_ size:) {input} ; {output} | Resulting size |
|---|
| GreenView | {375, 0} ; {375, 60} | {375, 60} |
| YellowView | {355, 0} ; {20, 40} | {355, 142} |
| RedView | {-20, 0} ; {0, 20} | {335, 122} |
| UILabel | {0, 0} ; {1828, 20} | {335, 122} |
The problem is, that we have view hierarchy with multiple levels of views that uses
autoSizeThatFitsfunction to calculate its size and the resulting size is wrong.Here is the example of view hierarchy and code to reproduce this bug.
Hierarchy
Code
Just add
GreenViewto your example view and layout it likegreenView.pin.top().start().end().sizeToFit(.width)The sizes of these views would be: