【问题标题】:How to make grid of elements in iOS?如何在 iOS 中制作元素网格?
【发布时间】:2012-11-10 09:20:50
【问题描述】:

所以我打算在 xCode 中为 iOS 制作一个应用程序(去看看),我很好奇人们会建议在这里做什么。

我的目标是创建一个尺寸为 mxn 的按钮网格。我最近制作了一个井字游戏变体,其中我有一个整体 9x9 的按钮网格。创建每个按钮是一项非常繁琐的工作。

是否有更简单的方法通过代码创建所有这些按钮?

【问题讨论】:

  • 这将是一个很好的观点......编辑iOS,抱歉。

标签: ios


【解决方案1】:

使用for 循环来创建按钮。可能是以下几种为 3X3 网格创建按钮

CGFloat xAxis,yAxis,bWidth,bHeight;
xAxis = 0.0;
yAxis = 0.0;
bWidth = 150.0;
bHeight = 44.0;
int numberOfRows = 3;
int numberOfColumns = 3;
int number = 1;

for(int i=0;i<numberOfRows;i++)
{
     for(int j=0;j<numberOfColumns;j++)
   {
      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(xAxis, yAxis, bWidth, bHeight)];
      button.titleLabel.text = [NSString stringWithFormat:@"%@",number];
      number++;
      [self.view addSubview:button];
      xAxis = xAxis  + bWidth + 5.0;
   }
   xAxis = 0;
   yAxis = yAxis +  bHeight + 5.0;
}

【讨论】:

    【解决方案2】:

    IBOutletCollections 是一个很好的候选者,如果你有合理数量的按钮。然后,您可以遍历所有这些以应用相同的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 2016-10-27
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      相关资源
      最近更新 更多