gpt4 book ai didi

objective-c - 在 NSArray 中创建 UIVews 网格

转载 作者:行者123 更新时间:2023-11-29 11:05:10 24 4
gpt4 key购买 nike

我想做的是对齐数组中网格中的 View 。我已经创建了 View 和数组。

for (int i = 0; i < [directoryContents count]; i++){


UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,120)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
button.accessibilityIdentifier = [directoryContents objectAtIndex:i];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
label.textAlignment = NSTextAlignmentCenter;
label.text = [directoryContents objectAtIndex:i];



[containerView addSubview:button];
[containerView addSubview:label];

[self.view addSubview:containerView];

[_containerViewArray addObject:containerView];



}
[self layoutViews:_containerViewArray inView:self.view];

我所做的工作是将它们彼此对齐我遇到的问题是我需要让它们包裹起来而不是离开边缘。我正在为他们做的是这个

    - (void)layoutViews:(NSArray *)views inView:(UIView *)contentView
{

NSInteger overallWidth = 0;
for ( UIView *view in views ) {
overallWidth += view.frame.size.width;
}

CGSize contentSize = contentView.frame.size;

CGFloat startOffset = (contentSize.height - overallWidth)/2.0;
CGFloat offset = startOffset;
for ( UIView *view in views ) {
CGRect frame = view.frame;
frame.origin.x = offset;
view.frame = frame;
offset += view.frame.size.width;
}
}

最佳答案

如果我们假设我们有一个部分数组(您的 View )并且您需要每行布局 3 个项目,则 View 的大小为 98*98,水平间距为 6.5px,垂直间距为 8px

for (int i = 0; i< [sectionsArray count]; i++) {
int row = (int)i/3;
int col = i%3;
float x = 6.5 + (104.5*col);
float y = 8 + (106*row);
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 98, 98)];
sectionView.backgroundColor = [UIColor clearColor];
//button
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[sectionButton setTintColor:[UIColor grayColor]];
sectionButton.frame = CGRectMake(0, 0, 98, 98);
sectionButton.tag = i+100;
[sectionButton addTarget:self action:@selector(showSectionDetail:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:sectionButton];

//adding title
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, 98, 20)];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.font=[UIFont fontWithName:@"Helvetica" size:14];
titleLabel.textColor = [UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0];
titleLabel.text = [[sectionsArray objectAtIndex:i] objectForKey:@"Title"];
[sectionView addSubview:titleLabel];
[titleLabel release];
[scroll addSubview:sectionView];
[sectionView release];
}
int numberOfRows;
if ([sectionsArray count]/3.0f == 0) {
numberOfRows = [sectionsArray count]/3;
}else{
numberOfRows = [sectionsArray count]/3 +1;

}
scroll setContentSize:CGSizeMake(320, numberOfRows*106);

关于objective-c - 在 NSArray 中创建 UIVews 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com