Objective c 使用数组创建视图
我想以编程方式创建视图,矩阵:10X10=100个视图。我已经创建了IB,但它必须用Xcode编程。我该怎么做 对于一个视图创建这个,对于10X10个视图我不知道。我怎样才能做到所有的观点Objective c 使用数组创建视图,objective-c,arrays,xcode,Objective C,Arrays,Xcode,我想以编程方式创建视图,矩阵:10X10=100个视图。我已经创建了IB,但它必须用Xcode编程。我该怎么做 对于一个视图创建这个,对于10X10个视图我不知道。我怎样才能做到所有的观点 UIView *tempView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 660, 60, 60)]; [self.view addSubview:tempView1]; rd
UIView *tempView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 660, 60, 60)];
[self.view addSubview:tempView1];
rd = 0;
gr = 0.5;
bl =0;
tempView1.backgroundColor = [UIColor colorWithRed:rd green:gr blue:bl alpha:1.0];
这将为您带来好处:
for (int i = 0; i < amountOfViewsHorizontally; i++)
{
for (int j = 0; j < amountOfViewsVertically; j++)
{
UIView *someView = [[UIView alloc] initWithFrame:CGRectMake((i*widthOfView), (j*heightOfView), widthOfView, heightOfView)];
rd = 0;
gr = 0.5;
bl =0;
someView.backgroundColor = [UIColor colorWithRed:rd green:gr blue:bl alpha:1.0];
UILabel *someLabel = [[UILabel alloc] init];
someLabel.frame = CGRectMake(0,0,50,20); //this will add a label in all the upperleft corners (point(0,0)) of every view with width 50 and height 20.
[someView addSubView:someLabel];
//[someViewArray addObject:someView]; //add view to an array
[self.view addSubview:someView];
}
}
for(int i=0;i
您可能希望将它们放入某种数组中,以便以后可以访问它们
如果希望将UILabel之类的组件添加到这些视图中。您必须使用这些视图的intern坐标系。这将为您提供以下技巧:
for (int i = 0; i < amountOfViewsHorizontally; i++)
{
for (int j = 0; j < amountOfViewsVertically; j++)
{
UIView *someView = [[UIView alloc] initWithFrame:CGRectMake((i*widthOfView), (j*heightOfView), widthOfView, heightOfView)];
rd = 0;
gr = 0.5;
bl =0;
someView.backgroundColor = [UIColor colorWithRed:rd green:gr blue:bl alpha:1.0];
UILabel *someLabel = [[UILabel alloc] init];
someLabel.frame = CGRectMake(0,0,50,20); //this will add a label in all the upperleft corners (point(0,0)) of every view with width 50 and height 20.
[someView addSubView:someLabel];
//[someViewArray addObject:someView]; //add view to an array
[self.view addSubview:someView];
}
}
for(int i=0;i
您可能希望将它们放入某种数组中,以便以后可以访问它们
如果希望将UILabel之类的组件添加到这些视图中。您必须使用这些视图的内部坐标系。有关动态更详细的布局、行、高度的示例,请查看。它不仅显示了如何根据大小和屏幕大小动态布局框,还显示了动态重新排序的工作方式 有关更多详细信息,请参阅本文,但关键方法是
- (void) layoutBoxesWithRowCount:(NSInteger)rowCount {
double xPos = kBoxSpacer;
double yPos = kBoxSpacer;
int boxCount = 0;
for(LOBox *box in boxes) {
CGRect frame = [box frame];
frame.origin.x = xPos;
frame.origin.y = yPos;
[box setFrame:frame];
xPos += kBoxDimension + kBoxSpacer;
boxCount++;
if(boxCount == rowCount) {
boxCount = 0;
xPos = kBoxSpacer;
yPos += kBoxDimension + kBoxSpacer;
}
}
}
完整记录有关更详细的布局、行、高度动态示例,请查看。它不仅显示了如何根据大小和屏幕大小动态布局框,还显示了动态重新排序的工作方式 有关更多详细信息,请参阅本文,但关键方法是
- (void) layoutBoxesWithRowCount:(NSInteger)rowCount {
double xPos = kBoxSpacer;
double yPos = kBoxSpacer;
int boxCount = 0;
for(LOBox *box in boxes) {
CGRect frame = [box frame];
frame.origin.x = xPos;
frame.origin.y = yPos;
[box setFrame:frame];
xPos += kBoxDimension + kBoxSpacer;
boxCount++;
if(boxCount == rowCount) {
boxCount = 0;
xPos = kBoxSpacer;
yPos += kBoxDimension + kBoxSpacer;
}
}
}
完整的评论欢迎你^^别忘了投票/接受对你有用的答案。此外,我想在我使用的视图中添加标签标签=[[UIView alloc]initWithFrame:CGRectMake((iwidthOfView),630+(jheightOfView),50,20)];但是看不到任何标签:(您尝试添加的UILabel使用您添加它们的视图的坐标系。someLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,50,20)];应该做到这一点。欢迎你^^别忘了投票/接受对你有用的答案。此外,我想给我使用的视图添加标签someLabel=[[UIView alloc]initWithFrame:CGRectMake((iwidthOfView),630+(jheightOfView),50,20)];但看不到任何标签:(您试图添加的UILabel使用您添加它们的视图的坐标系。someLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,50,20)];应该可以实现这一点。