鉴于以下图片:
三个标签:“TODAY,AVERAGE,LAST WEEK”在我的ViewController中动态定位。当应用程序部署到其他设备位置不正确的设备(即iPhone 6 Plus)时,这就成了一个问题。
我知道能够使用AutoLayout但是我想使用用于生成3个活动条的库来正确定位它们。
我正在使用的库是:CHCircleGauge。
我尝试将以下内容添加到此库中,但不确定使用哪种约束来将它们放置在此图像中的位置。
这是我到目前为止使用的代码,它是same code,用于定位水平和垂直居中的标签“6hrs”。
NSDictionary *valueLabelDictionary = @{@"valueText" : self.valueTextLabel};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[valueText]|" options:0 metrics:nil views:valueLabelDictionary]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[valueText]|" options:0 metrics:nil views:valueLabelDictionary]];
我只是添加了以下内容,至少能够使用动态约束,并且可以成功地将标签放在同一个位置。但问题是我不太确定如何将它们完美地定位在上图中。
NSDictionary *titleLabelDictionary = @{@"titleText" : self.titleTextLabel};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[titleText]|" options:0 metrics:nil views:titleLabelDictionary]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[titleText]|" options:0 metrics:nil views:titleLabelDictionary]];
非常感谢任何帮助。我仍然试图了解AutoLayout和约束如何工作。
更新
为了显示三个环,我使用以下内容:
var barView1 = CHCircleGaugeView(frame: CGRectMake(0, 0, 245, 245))
barView1.center = CGPointMake(self.gaugeView.frame.size.width / 2, self.gaugeView.frame.size.height / 2)
barView1.gaugeWidth = 16
barView1.gaugeTintColor = UIColor(red: 255/255, green: 202/255, blue: 76/255, alpha: 1)
barView1.trackWidth = barView1.gaugeWidth
barView1.trackTintColor = UIColor(red: 255/255, green: 202/255, blue: 76/255, alpha: 0.1)
self.gaugeView.addSubview(barView1)
barView1.setValue(0.10, animated: true)
然后我再添加两个尺寸较小的(以gaugeView
为中心)以适应更大的戒指。
更新2:
我最终使用了以下constant
填充:
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleTextLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:-9.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleTextLabel
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:-14.0]];
答案 0 :(得分:3)
看起来你是自动布局的新手,所以我会试着指出你正确的方向,而不是全部放弃。
垂直约束
将每个标签的Y限制在环的顶部而不是中心。将每个标签的顶部约束constant
略有不同设置,使它们看起来堆叠在一起。 constant
应为trackWidth * (labelIndexNumber)
。因此,它们最终会成为0
,16
和32
。
这样,如果您更改trackWidth
,您的标签会自动调整。您可能需要略微使用我的数字才能使填充正确。
水平约束
将每个标签的右侧约束到圆形视图的中心。我不认为你可以使用VFL;你必须使用详细的[NSLayoutConstraint constraintWithItem:…
语法。
标签设置
您的标签文字应该是右对齐的。您可能需要增加水平抗压力,具体取决于您的其他视图,但如果可以,请避免使用。