2013-03-02 37 views
0

Screenshot 1 Screenshot 2 Screenshot 3UI按鈕網格視圖 - 細胞(UIButtons)之間的間距

我不知道我怎麼會在網格中的UI按鍵之間的間距設置大小。目前我知道如何改變按鈕的大小,但是這會改變網格的整體大小,而不僅僅是按鈕。包括此刻的應用程序截圖(忽略UI按鈕邊框)。代碼如下:

- (void)setupBoard:(GameBoard *)board; 
{ 
NSUInteger rows = [[currentGame mainBoard] rows]; 
NSUInteger cols = [[currentGame mainBoard] columns]; 
CGSize cellSize = { gridView.frame.size.width/ cols, gridView.frame.size.height/ rows   }; 
CGPoint insertPoint = CGPointZero; 

for (int x = 0; x <= rows; x++) { 
    for (int y = 0; y <= cols; y++) { 
     UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     CGRect newButtonFrame = { insertPoint, cellSize }; 
     [newButton associateValue:[NSNumber numberWithInt:x] withKey:@"xrow"]; 
     [newButton associateValue:[NSNumber numberWithInt:y] withKey:@"ycol"]; 
     [newButton setFrame:newButtonFrame]; 
     [newButton setSelected:NO]; 
     [newButton setBackgroundColor:[UIColor colorWithRed:(164.0/255.0) green:(240.0/255.0) blue:(254.0/255.0) alpha:1.0]]; 
     [[newButton layer] setCornerRadius:5.0f]; 
     [[newButton layer] setBorderColor:[[UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:0.5] CGColor]]; 
     [[newButton layer] setBorderWidth:5.0f]; 
     [newButton addTarget:self action:@selector(toggleCellState:) forControlEvents:UIControlEventTouchUpInside]; 
     [gridView addSubview:newButton]; 
     [newButton setAlpha:0.10]; 
     insertPoint.x += cellSize.width; 
    } 
    insertPoint.x = 0; 
    insertPoint.y += cellSize.height; 
} 

}

,如果你想看到更多的代碼,不要猶豫。

回答

0

您可以使用下面的代碼在按鈕的每一側插入3的填充。這包括將單元尺寸減小6.

CGFloat padding = 3; 
    cellSize.width -= 2*padding; //new code 
    cellSize.height -= 2*padding; //new code 

    for (int x = 0; x <= rows; x++) { 
     insertPoint.y += padding; //new code 

     for (int y = 0; y <= cols; y++) { 
      insertPoint.x += padding; //new code 

      UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      CGRect newButtonFrame = { insertPoint, cellSize }; 
      [newButton associateValue:[NSNumber numberWithInt:x] withKey:@"xrow"]; 
      [newButton associateValue:[NSNumber numberWithInt:y] withKey:@"ycol"]; 
      [newButton setFrame:newButtonFrame]; 
      [newButton setSelected:NO]; 
      [newButton setBackgroundColor:[UIColor redColor]]; 
      [[newButton layer] setCornerRadius:5.0f]; 
      [[newButton layer] setBorderColor:[[UIColor blueColor] CGColor]]; 
      [[newButton layer] setBorderWidth:5.0f]; 
      [newButton addTarget:self action:@selector(toggleCellState:) forControlEvents:UIControlEventTouchUpInside]; 
      [gridView addSubview:newButton]; 
      [newButton setAlpha:0.10]; 

      insertPoint.x += padding+cellSize.width; //updated code 
     } 
     insertPoint.x = 0; 

     insertPoint.y += padding+cellSize.height; //updated code 
    } 
+0

非常感謝Rakesh。非常感激。 – 2013-03-08 20:10:29

+0

它工作?我沒有試過代碼.. – Rakesh 2013-03-09 05:28:38

+0

是的,它很有用,謝謝。 – 2013-03-09 07:29:52

相關問題