Ios NSLayoutConstraint在循环中用于
我试图通过Ios NSLayoutConstraint在循环中用于,ios,objective-c,for-loop,uilabel,constraints,Ios,Objective C,For Loop,Uilabel,Constraints,我试图通过for循环创建UILabel,该循环从NSArray获取NSInteger。循环工作正常,但我不能看到所有顶点一个接一个。我肯定是弄错了约束。你能帮我吗 基于此代码,我发布了如何在for循环中修复NSLayoutConstraint问题的帖子 NSInteger max = [[_yValueArray valueForKeyPath:@"@max.self"] integerValue]; NSInteger min = [[_yValueArray valueForKeyPath:
for
循环创建UILabel
,该循环从NSArray
获取NSInteger
。循环工作正常,但我不能看到所有顶点一个接一个。我肯定是弄错了约束。你能帮我吗
基于此代码,我发布了如何在for
循环中修复NSLayoutConstraint
问题的帖子
NSInteger max = [[_yValueArray valueForKeyPath:@"@max.self"] integerValue];
NSInteger min = [[_yValueArray valueForKeyPath:@"@min.self"] integerValue];
for (NSInteger value = max; value >= min; value -=1) {
UILabel *valueLabel = [[UILabel alloc] init];
valueLabel.textColor = [UIColor lightGrayColor];
valueLabel.backgroundColor = [UIColor redColor];
valueLabel.textAlignment = NSTextAlignmentLeft;
valueLabel.font = [UIFont defaultAppFontWithSize:10];
valueLabel.translatesAutoresizingMaskIntoConstraints = NO;
valueLabel.text = [self yValueString:value];
valueLabel.tag = value;
[yAxisView addSubview:valueLabel];
[yAxisView addConstraint:[NSLayoutConstraint constraintWithItem:valueLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:valueLine attribute:NSLayoutAttributeLeft multiplier:1 constant:-2]];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:valueLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:yAxisView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
[yAxisView addConstraint:topConstraint];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:valueLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:valueLine attribute:NSLayoutAttributeHeight multiplier:1 constant:10];
[yAxisView addConstraint: height];
// topConstraint.constant += height.constant;
}
我想得到的是这样的条形图。。。此时,我想得到您在图像中看到的Y轴的结果…我收集到的问题是垂直标记左轴 如果目标是将标签添加到左边缘,则可以将
centerY
约束设置为其superview的centerY
的倍数。唯一的诀窍是它不喜欢centerY
的乘法器使用0
,因此对于顶部的乘法器,我只需将centerY
设置为等于其superview的顶部。因此:
NSInteger max = 10;
for (NSInteger i = 0; i <= max; i++) {
UILabel *label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = false;
label.text = [NSString stringWithFormat:@"%ld", (long)i];
[self.chartDataView addSubview:label];
CGFloat multiplier = (CGFloat)(max - i) * 2.0 / (CGFloat)max;
[self.chartDataView addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.chartDataView attribute:NSLayoutAttributeLeft multiplier:1 constant:-5]];
if (multiplier == 0) {
[self.chartDataView addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.chartDataView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
} else {
[self.chartDataView addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.chartDataView attribute:NSLayoutAttributeCenterY multiplier:multiplier constant:0]];
}
}
NSInteger max=10;
对于(NSInteger i=0;i@Rob我正在尝试创建一个条形图。此时,我正在构建一个Y轴,其中包含18到30的参考值…在循环中,我想得到18到30之间的所有数字,并将它们放在标签中,这些数字必须垂直放置。从顶部开始30,然后29,然后28…等等,直到达到18in我需要这些标签,因为当我用表示图形栏的UIView创建X轴时,我可以根据标签的文本和插入到条形图中的其他值生成报告。我编辑了我的帖子。我已经用两个collectionView创建了所有内容,我几乎可以完成我的项目……我有了垂直填充的最大值一个单元格(图表栏)但我的最小值有问题…如果我成功找到帮助,我将解决我的所有问题:(但我找不到任何人可以帮助我解决最小值的小问题你最近输入的帖子已被删除为什么?)??