標籤:row main pre view sub add font tle max
#define ScreenW [UIScreen mainScreen].bounds.size.width#define ScreenH [UIScreen mainScreen].bounds.size.height
1 // 資料 2 NSArray *images = @[@"publish-boke", @"publish-weixin", @"publish-QQ", @"publish-offline"]; 3 NSArray *titles = @[@"發部落格", @"發", @"發QQ",@"離線下載"]; 4 5 // 中間的6個按鈕 6 int maxCols = 3; 7 CGFloat buttonW = 72; 8 CGFloat buttonH = buttonW + 30; 9 CGFloat buttonStartY = (ScreenH - 2 * buttonH) * 0.5;10 CGFloat buttonStartX = 20;11 CGFloat xMargin = (ScreenW - 2 * buttonStartX - maxCols * buttonW) / (maxCols - 1);12 for (int i = 0; i<images.count; i++) {13 VerticalButton *button = [[VerticalButton alloc] init];14 // 設定內容15 button.titleLabel.font = [UIFont systemFontOfSize:14];16 [button setTitle:titles[i] forState:UIControlStateNormal];17 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];18 [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];19 20 // 設定frame21 button.width = buttonW;22 button.height = buttonH;23 int row = i / maxCols;24 int col = i % maxCols;25 button.x = buttonStartX + col * (xMargin + buttonW);26 button.y = buttonStartY + row * buttonH;27 [self.view addSubview:button];28 }
iOS代碼實現九宮格