How To Get Leading Space To Be 1/3 The Width Of the SuperView

半腔热情 提交于 2019-12-11 22:21:13

问题


I am trying to set an icon to be 1/3 along the width of the superview.

However when I set the width it's only give me hard values as the option.

Is it possible to set the x position of an object to be 1/3rd the width through the interface builder?

Thanks

Update:

Trying to set a .leading against a .width does not appear to be an option for leading:


回答1:


You should make the constraint between the icon's centerX and the superview's trailing. In the images below, first, and third refer to the first and third icons. The constraints will put the first one 1/3 of the way from the left edge, and the third one 1/3 of the way in from the right edge.




回答2:


You can constrain the widths to be equal with a multiplier of 1/3

Sorry misread question. You could do something like

// self.imageView is a reference to the image view (or view, whatever it is)
// and self.leadingConstraint is a constraint on the imageView's leading/X constraint
// probably have to recalculate on rotation (maybe, didn't test)
CGFloat constant = self.imageView.superview.frame.size.width / 3;
self.leadingConstraint.constant = constant;
[self.view layoutIfNeeded];

In IB

Not perfect but ¯_(ツ)_/¯



来源:https://stackoverflow.com/questions/26477049/how-to-get-leading-space-to-be-1-3-the-width-of-the-superview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!