My app have a basic login screen, an outer box and in it, some text fields and buttons. I set the box to fill the screen. However, on some devices this box will be too big. How do I set maximum width and height to a view?
You can use auto-layout constraints so that the box adapts to the screen size, but does not exceed a given width and height. To do this, set a "less than or equal to" constraint on the width and height.
You can do this programmatically
some_view.widthAnchor.constraint(lessThanOrEqualTo: view.widthAnchor, multiplier: 0.45)
some_view.heightAnchor.constraint(lessThanOrEqualTo: view.widthAnchor multiplier: 0.45)
0.45
is basically saying the width and height of some_view will be at maximum 45% of the width of the super view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With