0
Я добавляю subviews к UIScrollView
(которые являются UIVewController
), и они не образуют сетку 2 x 2
, как они предполагают. Вот некоторые из кода, который я использую:Добавление subviews в UIScrollView в сетке.
-(void)layoutSubviews{
BOOL blurbsHaveBorder = YES;
int blurbCount = 3;
int blurbsPerRow;
int viewWidth = gridView.bounds.size.width;
int viewHeight = gridView.bounds.size.height;
int blurbWidth = 320;
int blurbHeight = 320;
int spaceWidth = round((viewWidth -blurbWidth * blurbsPerRow)/(blurbsPerRow + 1));
int spaceHeight = spaceWidth;
int rowCount = ceil(blurbCount/(float)blurbsPerRow);
int rowHeight = blurbHeight + spaceHeight;
if (blurbsPerRow < 1) blurbsPerRow = 1;
NSInteger rowsPerView = viewHeight/rowHeight;
NSInteger topRow = gridView.contentOffset.y/rowHeight;
NSInteger bottomRow = topRow + rowsPerView;
NSInteger startAtIndex = topRow * blurbsPerRow;
NSInteger stopAtIndex = (bottomRow * blurbsPerRow) + blurbsPerRow;
if (stopAtIndex > blurbCount) stopAtIndex = blurbCount;
int x = spaceWidth;
int y = spaceHeight + (topRow * rowHeight);
int i;
/* add new subviews. */
for (int i = startAtIndex; i < stopAtIndex; i++) {
if (i >= 0) {
blurbItem = [[BlurbItem alloc] initWithFrame:CGRectMake(5,5,blurbWidth,blurbHeight)
andHasBorder:blurbsHaveBorder];
[gridView addSubview:blurbItem.view];
[blurbItem release];
}
}
/* adjust the position. */
if ((i+1) % blurbsPerRow == 0) {
/* start new row. */
x = spaceWidth;
y += blurbHeight + spaceHeight;
} else {
x += blurbWidth + spaceWidth;
}
}
Да, по умолчанию будет 0. – WrightsCS
Я удивлен, что вы не получаете разницу по нулевой ошибке. Кроме того, вы используете его, не инициализируя его в первую очередь. – donkim