How does the UIViewLayoutSizeFitting method work?

In the documentation:

Returns the size of the view that satisfies the constraints that it has.

OK, I create a view in a storyboard and add restrictions:

yellowview.leading = self.view.leading

yellow view.trailing = self.view.trailing

yellow view.bottom = self.view.bottom

yelloview.height = 128

enter image description here

Now I print systemLayoutSizeFittingSize, which I expect as width UIScreenand 128 height. but the result:

====UILayoutFittingCompressedSize==
(0.0, 128.0)
====UILayoutFittingExpandedSize==
(10000.0, 128.0)

========================================

Then I create a view and add a restriction for it through the code:

    let btn :UIButton = {
        let btn = UIButton(type: .Custom)
        btn.backgroundColor = UIColor.redColor()
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(btn)
    btn.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor, constant: 100).active = true
    btn.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor, constant: -100).active = true
    btn.topAnchor.constraintEqualToAnchor(self.view.topAnchor, constant: 100).active = true
    btn.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor, constant: -100).active = true
}

He shows well:

enter image description here

Now print it systemLayoutSizeFittingSize:

=====UILayoutFittingExpandedSize==
(30.0, 34.0)
===UILayoutFittingCompressedSize==
(30.0, 34.0)

Which I expect to be UIScreensize: 100.

Why am I getting these strange values? Or do I not understand the document?

+4
source share

Source: https://habr.com/ru/post/1649430/


All Articles