IOS 九宮格布局實現(xiàn)方法
以前剛開始搞iOS的時候大部分都是通過計算frame來布局視圖,搞著搞著貌似都是用自動布局來搞定了,因為自動布局實在太方便、太好用了,所以當(dāng)我昨天突然回看以前代碼的時候突然看到了以前寫的九宮格布局,感覺很多東西都忘了,所以今天特意在這里記錄一下,并且通過幾個簡單的宏定義來完成布局的需求,具體大家看代碼吧,都有注釋 很好懂:
// // ButtonContainerView.h // chemuchao // // Created by 遇見遠(yuǎn)洋 on 16/3/7. // Copyright © 2016年 zhaoxiaolu. All rights reserved. // #import <UIKit/UIKit.h> //按鈕點擊block typedef void(^spitlotBtnClick)(UIButton * btn); @interface ButtonContainerView : UIView @property (nonatomic,copy)spitlotBtnClick spitlotBlock;/**<<#展示對話內(nèi)容的tableview#>*/ @end
這里給大家推薦一個寫注釋的好方法吧,在聲明屬性的時候,我們?nèi)绻朐趧e的地方調(diào)用這個屬性的時候在下方有提示 如圖:
只需要跟我在上面聲明屬性的時候一樣 在最后加上
/**<這是要寫的提示文字*/
使用這種方式聲明的屬性,在外面調(diào)用的時候就會有提示,好像跑題了,接下來點M的代碼吧:
// // ButtonContainerView.m // chemuchao // // Created by 遇見遠(yuǎn)洋 on 16/3/7. // Copyright © 2016年 zhaoxiaolu. All rights reserved. // #import "ButtonContainerView.h" #import "UIView+Extension.h" //狀態(tài)欄高度 #define kStateHeight 20 //總行數(shù) #define kRows 2 //總列數(shù) #define kCols 4 //九宮格個數(shù) #define kCount 8 //九宮格之間的間隙 #define kMargin 5 //字體大小 #define kFont15 [UIFont systemFontOfSize:15] @interface ButtonContainerView () @property (nonatomic,strong)NSMutableArray * btns; @property (nonatomic,strong)NSArray * btnTitles; @end @implementation ButtonContainerView - (NSMutableArray *)btns { if (!_btns) { _btns = [NSMutableArray array]; } return _btns; } -(NSArray *)btnTitles { if (!_btnTitles) { _btnTitles = @[@"堵成狗",@"堵成翔",@"路太窄",@"沒燈",@"路不平",@"積水多",@"顛簸",@"路太臟"]; } return _btnTitles; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setupUI]; } return self; } - (void)setupUI { for (int i = 0; i < kCount; i++) { UIButton * btn = [[UIButton alloc]init]; [btn setTitle:self.btnTitles[i] forState:UIControlStateNormal]; [self addSubview:btn]; btn.layer.borderWidth = 1; btn.layer.borderColor = [UIColor redColor].CGColor; btn.titleLabel.font = [UIFont systemFontOfSize:13]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(spitlotBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.btns addObject:btn]; } } -(void)layoutSubviews { [super layoutSubviews]; [self.btns enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { UIButton * btn = obj; btn.tag = idx; //行號 NSUInteger row = idx/kCols; //列號 NSUInteger col = idx%kCols; CGFloat btnW = (self.width - kMargin*(kCols + 1))/kCols; CGFloat btnH = (self.height - kMargin*(kRows + 1))/kRows -10; CGFloat btnX = kMargin + col*(kMargin + btnW); CGFloat btnY = kMargin + row*(kMargin + btnH) + kStateHeight; btn.frame = CGRectMake(btnX, btnY, btnW, btnH); }]; } #pragma mark 按鈕點擊事件 - (void)spitlotBtnClick:(UIButton *)sender { NSAssert(self.spitlotBlock != nil, @"傳入的block不能為空"); //執(zhí)行block self.spitlotBlock(sender); } @end
你只需要更換幾個宏定義就可以定制你的九宮格布局了,例如總行數(shù)、總列數(shù)、九宮格個數(shù),簡單吧 復(fù)用性還是很高的,當(dāng)然對于使用自動布局的你來說,可以無視我。
希望通過此文能幫助大家開發(fā) IOS九宮格的開發(fā),謝謝大家對本站的支持!
相關(guān)文章
iOS 判斷頁面中的該填項是否填完整,改變按鈕狀態(tài)的方法
下面小編就為大家分享一篇iOS 判斷頁面中的該填項是否填完整,改變按鈕狀態(tài)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01iOS開發(fā)筆記--詳解UILabel的相關(guān)屬性設(shè)置
這篇文章主要介紹了iOS開發(fā)筆記--詳解UILabel的相關(guān)屬性設(shè)置,對初學(xué)者具有一定的參考價值,有需要的可以了解一下。2016-11-11詳解ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher)
本篇文章主要介紹了ios中的SQL數(shù)據(jù)庫文件加密 (使用sqlcipher),具有一定的參考價值,這里整理了詳細(xì)的代碼,感興趣的小伙伴們可以參考一下。2016-12-12