ios用程式碼表示手機介面幾行 幾列 的 小檢視
/
// ViewController.m
// 九宮格
//
// Created by hlz on 16/2/29.
// Copyright © 2016年 hlz. All rights reserved.
//
#import "ViewController.h"
CGFloat viewW;//小檢視的寬
CGFloat viewH;//小的VIEW的高
int count=20;//放的圖示的個數
int columns=3;//列數
CGFloat margin=20;//邊距
@interfaceViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
viewW=(CGRectGetWidth(self.view.frame)-(columns+1)*margin)/columns;//求 出小檢視的寬度
viewH=viewW;//設定高度等於寬度
for (int i=0; i
{
UIView *view=[[UIView alloc]init];//建立檢視
[self.view addSubview:view];
int row=i/columns;//獲取圖示的行下標
int col=i%columns;//獲取當前圖示的列下標
CGFloat viewX=margin+col*(viewW+margin);
CGFloat viewY=margin+row*(viewH+margin);
view.frame=CGRectMake(viewX, viewY, viewW, viewH);
view.backgroundColor=[UIColorgreenColor];
}
// Do any additional setup after loading the
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end