用循环添加多行、多列视图

用循环构建多视图的相关代码

- (viod) viewDidLoad
{
  [super loadView];
  UIView *view = [[[UIView alloc] init] autorelease];
  self.view = view;
  NSInteger line = 3;
  NSInteger row = 3;
  CGFloat width = 80;
  CGFloat height = 80;

//获取第一个视图的x,y坐标
  CGFloat x = (self.view.bounds.size.width - width * row) / (row + 1);
  CGFloat y = (self.view.bounds.size.height - height * line) / (line + 1);
  for (NSInteger i = 0; i < line * row; i++)
  {

//计算每一个视图坐标
    CGFloat x1 = width + (width + x) * (i % line);
    CGFloat y1 = height + (height + y) * (i / row);
    UIView *newView = [[[UIView alloc] initWithFrame:CGRectMake(x1, y1, width, height)] autorelease];
    newView.backgoundColor = [UIColor cyanColor];
    [view addSubView:newView];
  }

 }

posted on 2015-12-18 01:01  tang杰  阅读(136)  评论(0编辑  收藏  举报

导航