【问题标题】:UI Button Grid View - Spacing between Cells (UIButtons)UI 按钮网格视图 - 单元格之间的间距 (UIButtons)
【发布时间】:2013-03-03 18:07:10
【问题描述】:

我想知道如何设置网格中 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;
}

}

如果您想查看更多代码,请不要犹豫。

【问题讨论】:

    标签: iphone xcode uibutton grid spacing


    【解决方案1】:

    您可以使用以下代码在按钮的每一侧插入一个 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
       }
    

    【讨论】:

    • 非常感谢拉克什。非常感谢。