【发布时间】:2018-04-02 13:13:11
【问题描述】:
我创建了一个 UIView,然后添加了 Anchor 约束,但是当我想读取这些值时遇到了问题...
在这种情况下,如您所见,我创建了一个 NSLayoutConstraint 属性来获取我的 uiview 的锚点宽度......然后我创建了一个包含约束但我的 NSLog 总是返回零值的 CGFloat。
我哪里错了?如何获取分配给锚点的 UIView 的宽度值?
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**
【问题讨论】:
-
此时
self.widthAnchor的值是多少?self是否可能没有固有大小,并且它的框架尚未设置? -
@DonMag 简而言之,我试图使用 uiview 而不是 CALayer 来遵循本教程,但我仍然得到错误的值,我不知道为什么 raywenderlich.com/36288/how-to-make-a-custom-control
-
好吧……所以……正如我在第一条评论中所问的那样……那时
self.widthAnchor的价值是多少? -
nslog 上的光标位置是 20.5 但 self.currentValue 的值是 30
标签: ios uiview anchor nslayoutconstraint cgfloat