iOS--九宮格布局

來源:互聯網
上載者:User

標籤: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--九宮格布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.