自动布局 - 按百分比定位 [英] Autolayout - Position by Percentage

查看:21
本文介绍了自动布局 - 按百分比定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想沿着一条线绘制点.

I'd like to map dots along a line.

线条长度取决于设备和方向,并且始终在整个屏幕上拉伸.因此,使用相对百分比定位点对我来说最有意义.

The line length depends on device and orientation and is always stretched across the whole screen. Therfore it would make most sense to me to position the dots using a relative percentage value.

到目前为止,我只发现在某种点值中定义了约束(Doc)

So far I only found constraints being defined in some sort of point value (Doc)

是否也可以使用百分比值作为约束?关于如何以可扩展的方式定位这些点的任何想法......或者我是否需要手动"进行这种转换/定位?

Is it possible to use percentage values as constraints as well? Any ideas on how to position these dots in a scalable way ... or do I need do this conversion/positioning "manually"?

推荐答案

是的,您可以将点定位为视图宽度的一小部分.NSLayoutConstraint 方法,constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:,有一个 multiplier 参数,可以让你使用 superview 和它的 subview 之间的分数关系.超级视图的右边缘将是该视图的宽度(如果它是全宽视图,则为屏幕),因此,如果您创建如下所示的约束,该点将沿线定位在一小部分距离处:

Yes, you can position the dot as a fraction of the view's width. The NSLayoutConstraint method, constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:, has that multiplier parameter that lets you use a fractional relationship between a superview and its subview. The superview's right edge will be the width of that view (the screen if its a full width view), so if you create a constraint like below, the dot will be positioned at a fractional distance along the line:

-(void)viewDidLoad {
      [super viewDidLoad];
      [self.view removeConstraint:self.leftConDark];
      [self.view removeConstraint:self.leftConLight];
      NSLayoutConstraint *lcd = [NSLayoutConstraint constraintWithItem:self.darkButton 
                                                             attribute:NSLayoutAttributeCenterX 
                                                             relatedBy:NSLayoutRelationEqual 
                                                                toItem:self.view 
                                                             attribute:NSLayoutAttributeRight 
                                                            multiplier:.5 
                                                              constant:0];
      NSLayoutConstraint *lcl = [NSLayoutConstraint constraintWithItem:self.lightButton 
                                                             attribute:NSLayoutAttributeCenterX 
                                                             relatedBy:NSLayoutRelationEqual 
                                                                toItem:self.view 
                                                             attribute:NSLayoutAttributeRight 
                                                            multiplier:.9 
                                                              constant:0];
      [self.view addConstraints:@[lcd,lcl]];
}

在本例中,我定位了两个 UIButton(信息类型为暗和亮).我在 IB 中添加了它们,并将 IBOutlets 设置为它们必须位于视图左侧的约束(这是系统给我的,它可能位于右侧 - 没关系,因为你只是删除它们而已. 如果您在代码中制作点,则不需要这样做).在代码中,我删除了这些约束,然后添加了新的约束,将按钮的中心置于屏幕的 50% 和 90%.

In this example I'm positioning two UIButtons (info type dark and light). I added them in IB, and made IBOutlets to their constraints they have to the left side of the view (that's what the system gave me, it could have been to the right side -- it doesn't matter since you just delete them anyway. If you are making the dots in code, you wouldn't need to do this). In code I remove those constraints, then add new ones that will put the center of the buttons at 50% and 90% of the way across the screen.

这篇关于自动布局 - 按百分比定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆