標籤:targe rect stat 建立 ota black gre nim ext
[self rankWithTotalColumns:2 andWithAppW:180 andWithAppH:170];//九宮格布局- (void)rankWithTotalColumns:(int)totalColumns andWithAppW:(int)appW andWithAppH:(int)appH{ //總列數 int _totalColumns = totalColumns; //view尺寸 CGFloat _appW = appW; CGFloat _appH = appH; //橫向間隙 (控制器view的寬度 - 列數*應用寬度)/(列數 + 1) CGFloat margin = (self.view.frame.size.width - (_totalColumns * 180)) / (_totalColumns + 1); NSLog(@"%lu",(unsigned long)self.TitleList.count); for (int index = 0; index < self.TitleList.count; index++) { //建立一個小框框// UIView *appView = [[UIView alloc] init]; appView.backgroundColor = [UIColor whiteColor]; appView.layer.borderColor = [[UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1] CGColor]; appView.layer.borderWidth = 1; DataOldBody *item =[self.TitleList objectAtIndex:index]; // 添加圖片 UIButton *iconView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 180, 120)]; UIImage *normal = [UIImage imageNamed:item.CharPicName]; [iconView setBackgroundImage:normal forState:UIControlStateNormal]; //添加點擊事件 [iconView addTarget:self action:@selector(handTap:) forControlEvents:UIControlEventTouchUpInside]; [iconView setTag:item.CharId]; [appView addSubview:iconView]; //添加簡介資訊 UILabel *label = [[UILabel alloc] init]; label.text = item.CharBodyText; label.frame = CGRectMake(0, 123, 180, 45); label.font = [UIFont fontWithName:@"Kailasa" size:13]; label.textAlignment = NSTextAlignmentCenter; label.numberOfLines = 2; [appView addSubview:label]; if (moonAndSuns) { label.backgroundColor = [UIColor colorWithRed:54/255.0 green:54/255.0 blue:54/255.0 alpha:1]; label.textColor = [UIColor whiteColor]; }else{ label.backgroundColor = [UIColor whiteColor]; label.textColor = [UIColor blackColor]; } //建立結束// //計算框框的位置...行號列號從0開始 //行號 int row = index / totalColumns; //行號為框框的序號對列數取商 //列號 int col = index % totalColumns; //列號為框框的序號對列數取餘 CGFloat appX = margin + col * (appW + margin); // 每個框框靠左邊的寬度為 (平均間隔+框框自己的寬度) CGFloat appY = 20 + row * (appH + margin); // 每個框框靠上面的高度為 平均間隔+框框自己的高度 appView.frame = CGRectMake(appX, appY, _appW, _appH); [self.scrollView addSubview:appView]; }}-(void)handTap:(id)sender{ NSInteger i = [sender tag]; NSLog(@"%ld",(long)i); OldFirstBodyViewController *OldFirstBody = [self.storyboard instantiateViewControllerWithIdentifier:@"OldFirstBody"]; OldFirstBody.index = i; [self.navigationController pushViewController:OldFirstBody animated:YES]; }
iOS--九宮格布局