简体   繁体   中英

Constraints of Textfield conflicting with UIButton

I am still in the learning process of learning constraints, so bear with me:

In xCode Interface Builder, my UI looks like the following:

在此处输入图片说明

The top TextField has the following constraints set up:

在此处输入图片说明

And the reverse button on the right has the following constraints:

在此处输入图片说明

When I run it in the debugger, I get a misconfigured UI:

在此处输入图片说明

What exactly is happening here? Why is my reverse button increasing to a huge size and displacing to a weird position and why is the first TextField shrunk?

Ideally, I want my debugger UI to look like my Interface Builder in the first image. I am setting this all up through the interface builder and not programmatically.

Without more information, my first guess is that the intrinsic size of the image is causing conflicts with the constraints you've set up on the button on the right. This is most likely causing the constraint engine to remove your specified priorities, as when there are conflicts at runtime, it just starts removing constraints semi-randomly until there are not more conflicts.

You have a lot of things specified against the superview. Try not to use superview relative constraints unless you're really specifying constraints against the superview, and not other components within that view.

Always be sure to check log messages at runtime about constraint conflicts that lead to conflicts being removed, as that's almost always the reason for bizarre layouts like this.

Top TextField should have as follows:

top space to superview, leading space to superview, width and height

Button should have as follows:

top space to superview, trailing space to superview, width and height

2nd TextField should have as follows:

top space to top TextField, leading space to superview, equal width and equal height with top TextField

As your button had no height and width...this may take a intrinsic size of image for button and you may get conflicting output...

Here is the image and constraints...may be work for you...

在此处输入图片说明

if you click on the 界面生成器中的注释项

you have the option of simulating the device that you are working on while still in in interface builder by going into the attributes inspector of the game View Controller.

Simulate the device you are working on in interface builder and it should be certain that Ib matches the simulator(as long as a yellow error indicator is not showing next to the Game View controller title-- the error would say that ".... may be different at run time " or something like that).

The dimensions that are used in IB at the bottom in this case compact by compact determine the device and orientation(landscape or portraite) that the constraints made apply to. Regular by regular is universal constraints. Check my previous posts for more detail.

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