I subclass UIView and cannot find the right way to request / report its desired size. I expected to systemLayoutSizeFittingSizedo the trick, but it is never called. The layout is simple - I write on top and behave in my view and limit the end and bottom to be less than or equal to the view of the top level. Among all the functions, calibration is called only intrinsicContentSize, but it is not very useful.
import Foundation
import UIKit
class StatsView: UIView
{
override func drawRect(rect: CGRect)
{
let context=UIGraphicsGetCurrentContext()!
CGContextSetRGBFillColor(context, 0, 0, 1, 1)
CGContextFillRect(context, rect)
}
override func sizeThatFits(size: CGSize) -> CGSize
{
print("Called sizeThatFits with \(size)")
if(size.width>350)
{
return CGSize(width: 350,height: 50)
}
else
{
return CGSize(width: 50,height: 100)
}
}
override func systemLayoutSizeFittingSize(size: CGSize) -> CGSize
{
print("Called systemLayoutSizeFittingSize with \(size)")
return super.systemLayoutSizeFittingSize(size)
}
override func systemLayoutSizeFittingSize(size: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize
{
print("Called systemLayoutSizeFittingSize with \(size)")
if(size.width>350)
{
return CGSize(width: 350,height: 50)
}
else
{
return CGSize(width: 50,height: 100)
}
}
override func intrinsicContentSize() -> CGSize
{
super.intrinsicContentSize()
print("Called intrinsicContentSize")
return CGSize(width: 10,height: 50)
}
}
What is the right way to do this?
. , , . "" , , . , intrinsicContentSize .