用数组创建视图

发布于 2024-12-11 16:49:50 字数 486 浏览 0 评论 0原文

我想以编程方式创建视图, Matrix: 10X10 = 100 Views 。我已经创建了 IB,但它必须使用 Xcode 进行编程。我该怎么做?

对于一个视图创建这个,对于 10X10 视图我不知道。我怎样才能做到所有观点;

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];

I want to create views programmatically, Matrix: 10X10 = 100 views . I've created IB but it has to be programmed with Xcode. How can i do this?

For one view create this, for 10X10 views I don't know. How can i do all views;

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2024-12-18 16:49:50

这应该可以满足您的需要:

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];
    }
}

您可能希望将它们放入某种数组中,以便稍后可以访问它们。


如果您希望将 UILabel 之类的组件添加到这些视图中。您将必须使用这些视图的内部坐标系。

This should do the trick for you:

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];
    }
}

You might want to put them into some sort of array so you can access them at some later point.


If you want components like UILabel to be added to those views. You will have to use the intern coordinate system of those views.

情魔剑神 2024-12-18 16:49:50

有关更详细的动态布局、行、高度的示例,请查看 我的文章在这里。它不仅展示了如何根据尺寸和屏幕尺寸动态布局框,还展示了动态重新排序的工作原理。

有关更多详细信息,请参阅文章,但关键方法是...

- (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;
    }
  }
}

完整的文章 这里。

For an example of more detailed layout, row, height dynamically, check out my article here. It shows how you can not only dynamically layout boxes according to size and screen size but also how dynamic reordering works.

See the article for more details but the key approach is...

- (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;
    }
  }
}

Full write-up here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文