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

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

问题描述

我想沿着一条线图点。

I'd like to map dots along a line.

的线长度取决于设备和方向和在整个屏幕总是拉伸。 Therfore它将使最意义,我的位置使用的相对百分比点值。

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.

到目前为止,我只发现了某种点值被定义的约束(<一个href=\"https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/constraintFundamentals.html#//apple_ref/doc/uid/TP40010853-CH2-SW2\">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:属性:relatedBy:toItem:属性:事半功倍:恒:,有事半功倍的参数,使您使用的SuperView及其子视图之间的分数关系。在上海华的右边缘将是视图(画面如果它的全宽视图)的宽度,所以如果你创建像下面约束,点会被放置在沿线分数距离:

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]];
}

在这个例子中,我定位2 UIButtons(资讯型深色和浅色)。我加了他们的IB,并提出IBOutlets他们限制,他们不得不视图的左侧(这是系统给我的,它可能是右侧 - 这并不重要,因为你只是删除他们无论如何如果你正在在code的点,你不会需要做到这一点)。在code我删除这些约束,然后添加新的,将放在按钮的中心在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天全站免登陆