简体   繁体   中英

Constraints in Swift not working

I'm trying to add some constraints to my view, but everytime I switch the device, the view goes crazy. How can I fix this?

How I want it to look and how it looks like on the iPhone 8 Plus:

我希望它的外观以及在iPhone 8 Plus上的外观

How it looks like when I switch to iPhone SE:

切换到iPhone SE时的外观

I added these constraints:

  • 0 top
  • 0 left
  • 0 right
  • 686 bottom

If your bottom constraint is larger than the height of the device, naturally, it will be out of bounds .

Moreover, your constraints will be ambiguous if the screen's height is smaller than 686.

You'll need to have a height constraint with a constant of 50 to set up on the view rather than a bottom constraint.

It's not preferable to have such a huge value for any constraint. So when changing devices, the height of screens may change and your constraints will break.

The preferred way will be to create a Height constraint and set it to 686 and don't set the bottom constraint as it's not needed anymore. But the problem can arise here if the screen size is smaller, as the views on the bottom will not be visible. To overcome this you should make use of a scrollview and put this content view inside scroll view.

you can set the Height constraint of View instead of bottom constraint
or you can use the Size classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM