Ios 从锚点约束读取CGFloat
我创建了一个UIView,然后添加了锚定约束,但是当我想要读取值时,我遇到了一个问题 如您所见,在本例中,我创建了一个NSLayoutConstraint属性来获取uiview的锚定宽度。。。然后,我创建了一个包含约束的CGFloat,但我的NSLog总是返回一个零值 我错在哪里?如何获取分配给锚定的UIView的宽度值Ios 从锚点约束读取CGFloat,ios,uiview,anchor,nslayoutconstraint,cgfloat,Ios,Uiview,Anchor,Nslayoutconstraint,Cgfloat,我创建了一个UIView,然后添加了锚定约束,但是当我想要读取值时,我遇到了一个问题 如您所见,在本例中,我创建了一个NSLayoutConstraint属性来获取uiview的锚定宽度。。。然后,我创建了一个包含约束的CGFloat,但我的NSLog总是返回一个零值 我错在哪里?如何获取分配给锚定的UIView的宽度值 UIView *trackLine = [[UIView alloc] init]; trackLine.backgroundColor = [self trackL
UIView *trackLine = [[UIView alloc] init];
trackLine.backgroundColor = [self trackLineColor];
trackLine.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:trackLine];
[trackLine.topAnchor constraintEqualToAnchor:mediaTitle.bottomAnchor constant:25].active = YES;
[trackLine.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
[trackLine.heightAnchor constraintEqualToConstant:1].active = YES;
self.width = [trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:.8];
self.width.active = YES;
CGFloat trackLineLenght = self.width.constant;
NSLog(@"TRACK LINE %f", trackLineLenght );
NSLog结果:
**2017-10-21 17:10:35.096562+0200 [5501:1994879] TRACK LINE 0.000000**
好的-有点困惑 首先,不要使用“宽度”作为属性名称。。。非常混乱,因为宽度已经在很多地方使用了 那么,让我们假设您有:
@property NSLayoutConstraint *trackLineWidthConstraint;
.widthAnchor
实际上没有“锚的宽度是多少”的宽度。您定义约束的方式:
self.trackLineWidthConstraint = [trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:.8];
表示“将.trackLineWidthConstraint
属性设置为self
宽度的80%。因此,每当self
的实际宽度更改时,trackLine
视图的实际宽度将更改为新宽度的80%
.constant
为零。如果它不是零,则该值将在计算80%后相加。例如:
self.trackLineWidthConstraint = [trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:.8];
// if self is 200-pts wide, trackLine will be 160-pts
self.trackLineWidthConstraint.constant = 10
// trackLine width is now (200 * 0.8) + 10, or 170-pts
如果您想获得轨道线的当前宽度,可以从其.frame
(自动布局完成后)获取
希望这不会让它变得更混乱:)self.widthAnchor
在这一点上的值是什么?是否有可能self
没有一个固有的大小,并且它的框架还没有设置?@DonMag简单地说,我试图使用uiview而不是CALayer来学习本教程,但我还是弄错了值,我不知道为什么OK…所以…正如我在第一条评论中所问的那样…此时的self.widthAnchor
的值是多少?光标位置上的nslog是20.5,但self.currentValue是值是30 OK,但在自动布局之后如何获得帧?为什么我调用view.frame.size.width时从不给我一个值…是吗我调用帧的位置错误?如果我们可以进行2分钟的聊天,我将查看我的代码,以了解滑块不响应的原因…我疯了,在这里与我一起聊天(以前与其他人聊天过…)