iOS中丰嘉,多宮格的界面很常見饮亏,實現(xiàn)的方法也有很多種,例如除了常見的UICollectionView方式荐开,也還可以通過自定義一個UIView劝赔,在UIView中實現(xiàn)多宮格的創(chuàng)建,以下為我的工程中封裝的多宮格創(chuàng)建代碼杂伟,在此記錄一下仍翰。
完整代碼
1.自定義一個繼承UIView的類JiugonggeView
//JiugonggeView.h文件中實現(xiàn)
@interface JiugonggeView : UIView
//將點擊按鈕的事件傳回給上一層
@property (nonatomic, copy)void(^callBackTitle)(NSString *title);
//視圖的初始化
- (instancetype)initWithFrame:(CGRect)frame andBtnTitleArray:(NSArray *)array;
@end
//JiugonggeView.m文件中實現(xiàn)
#define IMAGENAMED(imgName) [UIImage imageNamed:imgName]
@implementation JiugonggeView
{
CGFloat rowFloat;
}
//視圖初始化
- (instancetype)initWithFrame:(CGRect)frame andBtnTitleArray:(NSArray *)array
{
self = [super initWithFrame:frame];
if (self)
{
//創(chuàng)建按鈕的邊框
rowFloat = 4.00;//這里表示行數(shù)為4
for (NSInteger i = 0; i < rowFloat; i ++)
{
//每行列數(shù)為3
for (NSInteger j = 0; j < 3; j ++)
{
CGFloat btnW = frame.size.width/3.00;
CGFloat btnH = frame.size.height/rowFloat-0.3;
CGFloat btnX = j*btnW;
CGFloat btnY = I*btnH;
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
[backView.layer setBorderColor:RGB_HEX(0xe3e3e3, 1).CGColor];
[backView.layer setBorderWidth:0.3];
[self addSubview:backView];
}
}
[self createBtn:array];
}
return self;
}
//創(chuàng)建按鈕
- (void)createBtn:(NSArray *)array
{
//計算行數(shù)
NSInteger myCount = [array count]/3;
if ([array count]%3 > 0)
{
myCount += 1;
}
//從數(shù)組中取標(biāo)題的索引
NSInteger index = 0;
//行循環(huán)
for (NSInteger i = 0; i <myCount; i ++)
{
//計算列數(shù)
NSInteger num = 3;
if (i == myCount-1 && [array count]%3 > 0)
{
num = [array count]%3;
}
//列循環(huán)
for (NSInteger j = 0; j < num; j ++)
{
//設(shè)置按鈕的位置參數(shù)
CGFloat btnW = self.frame.size.width/3.00;
CGFloat btnH = self.frame.size.height/rowFloat;
CGFloat btnX = j*btnW;
CGFloat btnY = I*btnH;
//創(chuàng)建按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(btnX, btnY, btnW, btnH)];
[btn setBackgroundColor:[UIColor clearColor]];
[btn addTarget:self action:@selector(btnFunction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
//設(shè)置按鈕中圖片文字
NSArray *ary = array[index];
[btn setTitle:ary[1] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightLight]];
[btn.titleLabel setNumberOfLines:0];
[btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setImage:IMAGENAMED(ary[0]) forState:UIControlStateNormal];
//設(shè)置按鈕中圖片在上文字在下
CGFloat offset = 14.0f;
btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, -btn.imageView.frame.size.height-offset/2, 0);
btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -btn.titleLabel.intrinsicContentSize.width);
//設(shè)置從數(shù)組中取標(biāo)題的索引
index ++;
}
}
}
//點擊按鈕
- (void)btnFunction:(UIButton *)sender
{
if (self.callBackTitle)
{
self.callBackTitle(sender.titleLabel.text);
}
}
2.在VC中調(diào)用
- (void)viewDidLoad
{
[super viewDidLoad];
[self createScrollView];
}
//創(chuàng)建視圖
- (void)createScrollView
{
NSArray *dataAry = @[@[@"res_ico_Report",@"按鈕1"],@[@"oth_ico_app",@"按鈕2"],@[@"oth_ico_dtc",@"按鈕3"],@[@"res_ico_batterydataplayback",@"按鈕4"],@[@"res_ico_dataplayback",@"按鈕5"]];
//創(chuàng)建功能按鈕視圖
JiugonggeView *funcView = [[JiugonggeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) andBtnTitleArray: dataAry];
[funcView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:funcView];
//點擊按鈕后的回調(diào)
[funcView setCallBackTitle:^(NSString *title)
{
[self funcButtonClickAction:title];
}];
}
//實現(xiàn)按鈕方法
- (void)funcButtonClickAction:(NSString *)title
{
}
效果圖
img_1.png
UICollectionView實現(xiàn)多宮格界面