我有一个水平3块的简单视图。每个块都是一个小图像和2个标签,块之间是一个小垂直条。在资源中,它们一个接一个地放置。我使用以下代码将各个图像和标签放在正确的位置:
- (void)layoutRankingFields
{
// remove lines
for (UIView *subview in [self subviews])
{
if (subview.tag == LINE1_TAG || subview.tag == LINE2_TAG)
{
[subview removeFromSuperview];
}
}
// we need a separator space on all sides of the three groups
float totalWeeks = self.imageWeeks.frame.size.width + [self widthForLabel:self.nrWeeks] + [self widthForLabel:self.labelWeeks];
float totalHighest = self.imageHighest.frame.size.width + [self widthForLabel:self.nrHighest] + [self widthForLabel:self.labelHighest];
float totalYear = self.imageYear.frame.size.width + [self widthForLabel:self.nrYear] + [self widthForLabel:self.labelYear];
float widthSeparator = (self.frame.size.width - (totalWeeks + totalHighest + totalYear + 8 /*spacing between nr and label field and 2 separator lines*/)) / 6;
// now reposition all fields
// weeks
float floatX = widthSeparator;
[self repositionSubView:self.imageWeeks xpos:floatX];
floatX = floatX + self.imageWeeks.frame.size.width;
[self repositionSubView:self.nrWeeks xpos:floatX];
floatX = floatX + [self widthForLabel:self.nrWeeks] + 2;
[self repositionSubView:self.labelWeeks xpos:floatX];
// vertical separator line
floatX = floatX + [self widthForLabel:self.labelWeeks] + widthSeparator;
UIView *line = [[UIView alloc] initWithFrame:(CGRectMake)(floatX, 0, 1, self.bounds.size.height)];
line.backgroundColor = [UIColor colorWithWhite:224/255.0f alpha:1.0f];
[line setTag:LINE1_TAG];
[self addSubview:line];
[line release];
// highest
floatX = floatX + 1 + widthSeparator;
[self repositionSubView:self.imageHighest xpos:floatX];
floatX = floatX + self.imageHighest.frame.size.width + 2;
[self repositionSubView:self.nrHighest xpos:floatX];
floatX = floatX + [self widthForLabel:self.nrHighest] + 2;
[self repositionSubView:self.labelHighest xpos:floatX];
// vertical separator line
floatX = floatX + [self widthForLabel:self.labelHighest] + widthSeparator;
line = [[UIView alloc] initWithFrame:(CGRectMake)(floatX, 0, 1, self.bounds.size.height)];
line.backgroundColor = [UIColor colorWithWhite:224/255.0f alpha:1.0f];
[line setTag:LINE2_TAG];
[self addSubview:line];
[line release];
// year
floatX = floatX + 1 + widthSeparator;
[self repositionSubView:self.imageYear xpos:floatX];
floatX = floatX + self.imageYear.frame.size.width;
[self repositionSubView:self.nrYear xpos:floatX];
floatX = floatX + [self widthForLabel:self.nrYear] + 2;
[self repositionSubView:self.labelYear xpos:floatX];
}
- (void)repositionSubView:(UIView*)view xpos:(float)x
{
CGRect frame = view.frame;
frame.origin.x = x;
view.frame = frame;
view.hidden = FALSE;
[view setNeedsLayout];
}
但是,在iOS 8上运行时,最后一个块放在前一个标签上。垂直条放置正确。我添加了一些日志记录来查看计算出的值,它们在iOS 7和8之间是相同的。
有什么建议吗?