简体   繁体   中英

Values,set in auto-layout constraints doesn't match element's position on subview

There is a custom UITableViewCell subclass with xib. all elements' constraints are set in IB 在此处输入图片说明

Let's take a look at Test label (UILabel element). It has 3 constraints: 在此处输入图片说明

The trailing space to superview(cell's content view) is set to 12 .

The leading space to superview is set to 12

The top space to superview is set to 5

But when I build and run, the real distance between left simulator's margin and label's leading side is 20 ! As well for top space - it is more then expected 5px

在此处输入图片说明

If I give to label long name - it gets resized and trailing space is positioned correctly -exactly to 12px, as expected.

The same situation is with 2 images below test label. While theirs leading spaces are set to 12, in reality they are distanced on 20 px from content view's left margin.

What can be the reason of issue?

By default in iOS 8 there is an 8px padding for margins (A view's margin property is UIEdgeInset with 8px padding for all four sides). When you set 12px for leading constrain the margin's 8px padding also will be added to the constrain, thats why your leading constrain is 20px.

Uncheck the " Relative to Margin " option for the leading constrain to fix this issue.

在此处输入图片说明

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