欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 中根據(jù)屏幕寬度自適應(yīng)分布按鈕的實例代碼

 更新時間:2016年11月16日 09:07:32   作者:Leemin_ios  
這篇文章主要介紹了iOS 中根據(jù)屏幕寬度自適應(yīng)分布按鈕的實例代碼,本文給大家分享兩種方式,代碼簡單易懂,需要的朋友可以參考下

 下載demo鏈接:https://github.com/MinLee6/buttonShow.git

屏幕擺放的控件有兩種方式,一種根據(jù)具體內(nèi)容變化,一種根據(jù)屏幕寬度變化。

下面我分別將兩個方式,用代碼的方式呈現(xiàn):

1:根據(jù)具體內(nèi)容變化

// 
// StyleOneViewController.m 
// buttonShow 
// 
// Created by limin on 15/06/15. 
// Copyright © 2015年 信諾匯通信息科技(北京)有限公司. All rights reserved. 
// 
#import "StyleOneViewController.h" 
#import "UIViewExt.h" 
//每列間隔 
#define KViewMargin 10 
//每行列數(shù)高 
#define KVieH 28 
#define KscreenW [UIScreen mainScreen].bounds.size.width 
#define Color(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0] 
@interface StyleOneViewController () 
{ 
UIButton *tmpBtn; 
CGFloat btnW; 
CGFloat btnViewHeight; 
} 
/* 存放按鈕的view */ 
@property(nonatomic,strong)UIView *btnsView; 
/* 按鈕上的文字 */ 
@property(nonatomic,strong)NSMutableArray *btnMsgArrays; 
@property (nonatomic,strong) NSMutableArray* btnIDArrays; 
/** 所有按鈕 */ 
@property(nonatomic,strong)NSMutableArray *allBtnArrays; 
/** 服務(wù)器提供按鈕標(biāo)簽 */ 
@property(nonatomic,strong)NSArray *tagInfoArray; 
//-------展示選中的文字 
/* 確認(rèn)按鈕 */ 
@property(nonatomic,strong)UIButton *sureButton; 
/* 文字 */ 
@property(nonatomic,strong)UILabel *showLabel; 
@end 
@implementation StyleOneViewController 
- (void)viewDidLoad { 
[super viewDidLoad]; 
self.view.backgroundColor = [UIColor whiteColor]; 
[self getTagMsg]; 
} 
-(void)getTagMsg 
{ 
self.btnMsgArrays = [[NSMutableArray alloc]init]; 
_allBtnArrays = [[NSMutableArray alloc]init]; 
self.btnIDArrays = [[NSMutableArray alloc]init]; 
self.tagInfoArray = @[@{@"id":@"1",@"tagmsg":@"味道很好味道很好"}, 
@{@"id":@"1",@"tagmsg":@"環(huán)境不錯"}, 
@{@"id":@"1",@"tagmsg":@"性價比高"}, 
@{@"id":@"1",@"tagmsg":@"位置好找"}, 
@{@"id":@"1",@"tagmsg":@"上菜快"}, 
@{@"id":@"1",@"tagmsg":@"菜量足"}, 
@{@"id":@"1",@"tagmsg":@"好吃"}, 
@{@"id":@"1",@"tagmsg":@"態(tài)度好,服務(wù)周到"} 
]; 
//挨個賦值 
for (int i=0; i<_tagInfoArray.count; i++) { 
NSDictionary *dict = _tagInfoArray[i]; 
[self.btnIDArrays addObject:dict[@"id"]]; 
[self.btnMsgArrays addObject:dict[@"tagmsg"]]; 
} 
[self createBtns]; 
} 
//創(chuàng)建按鈕 
-(void)createBtns{ 
//創(chuàng)建放置button的view 
self.btnsView = [[UIView alloc]initWithFrame:CGRectMake(10, 40, KscreenW-2*KViewMargin, 40)]; 
self.btnsView.backgroundColor = Color(237, 237, 237); 
[self.view addSubview:self.btnsView]; 
/** 
* 數(shù)組存放適配屏幕大小的每行按鈕的個數(shù) 
*/ 
NSMutableArray *indexbtns=[self returnBtnsForRowAndCol]; 
//統(tǒng)計按鈕View的高度 
btnViewHeight=indexbtns.count*(KVieH+KViewMargin)+10; 
//設(shè)置btnView的高度 
self.btnsView.height=btnViewHeight; 
NSInteger count=0; 
CGFloat Y; 
//循環(huán)創(chuàng)建按鈕 
for (int row=0; row<indexbtns.count; row++) { 
for (int col=0; col<[indexbtns[row]intValue]; col++) { 
CGFloat X; 
Y=10+row*(KViewMargin+KVieH); 
//按鈕的寬 
btnW=[self returnBtnWithWithStr:self.btnMsgArrays[count]]; 
if (tmpBtn&&col) { 
X=CGRectGetMaxX(tmpBtn.frame)+KViewMargin; 
}else{ 
X=KViewMargin+col*btnW; 
} 
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(X, Y, btnW, KVieH)]; 
[btn setTitle:self.btnMsgArrays[count] forState:UIControlStateNormal]; 
btn.titleLabel.font=[UIFont systemFontOfSize:12]; 
btn.layer.borderWidth=1; 
btn.layer.borderColor = Color(156,156,156).CGColor; 
[btn setTitleColor:Color(156, 156, 156) forState:UIControlStateNormal]; 
[btn setTitleColor:Color(202, 48, 130) forState:UIControlStateSelected]; 
btn.backgroundColor=[UIColor clearColor]; 
btn.layer.cornerRadius=2; 
btn.tag=[self.btnIDArrays[count] integerValue]; 
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; 
[self.allBtnArrays addObject:btn]; 
tmpBtn=btn; 
[self.btnsView addSubview:btn]; 
count+=1; 
} 
} 
//創(chuàng)建確認(rèn)按鈕 
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(KViewMargin, self.btnsView.bottom+40, KscreenW-2*KViewMargin, 44)]; 
[btn setTitle:@"確認(rèn)" forState:UIControlStateNormal]; 
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
btn.backgroundColor = Color(214, 116, 0); 
[btn addTarget:self action:@selector(showSelectedClick:) forControlEvents:UIControlEventTouchUpInside]; 
self.sureButton = btn; 
[self.view addSubview:btn]; 
//返回按鈕 
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake((KscreenW-80)*0.5, self.view.height-80, 80, 80)]; 
[btn2 setTitle:@"返回" forState:UIControlStateNormal]; 
[btn2 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; 
btn2.layer.cornerRadius = 40; 
btn2.clipsToBounds = YES; 
btn2.layer.borderColor = [UIColor purpleColor].CGColor; 
btn2.layer.borderWidth = 2; 
[btn2 addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:btn2]; 
} 
#pragma mark-返回view中有幾行幾列按鈕 
-(NSMutableArray*)returnBtnsForRowAndCol{ 
CGFloat allWidth = 0.0; 
NSInteger countW=0; 
NSMutableArray *indexbtns=[NSMutableArray array]; 
NSMutableArray *tmpbtns=[NSMutableArray array]; 
for (int j=0;j<self.btnMsgArrays.count;j++) { 
CGFloat width=[self returnBtnWithWithStr:self.btnMsgArrays[j]]; 
allWidth+=width+KViewMargin; 
countW+=1; 
if (allWidth>KscreenW-10) { 
//判斷第一行情況 
NSInteger lastNum=[[tmpbtns lastObject]integerValue]; 
[indexbtns addObject:@(lastNum)]; 
[tmpbtns removeAllObjects]; 
allWidth=0.0; 
countW=0; 
j-=1; 
}else{ 
[tmpbtns addObject:@(countW)]; 
} 
} 
if (tmpbtns.count!=0) { 
NSInteger lastNum=[[tmpbtns lastObject]integerValue]; 
[indexbtns addObject:@(lastNum)]; 
} 
return indexbtns; 
} 
-(CGFloat)returnBtnWithWithStr:(NSString *)str{ 
//計算字符長度 
NSDictionary *minattributesri = @{NSFontAttributeName:[UIFont systemFontOfSize:12]}; 
CGSize mindetailSizeRi = [str boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesFontLeading attributes:minattributesri context:nil].size; 
return mindetailSizeRi.width+12; 
} 
#pragma mark-按鈕點擊事件 
-(void)btnClick:(UIButton *)btn 
{ 
btn.selected = !btn.isSelected; 
if (btn.isSelected) { 
btn.layer.borderColor = Color(202, 48, 130).CGColor; 
}else 
{ 
btn.layer.borderColor = Color(156, 156, 156).CGColor; 
} 
} 
-(void)showSelectedClick:(UIButton *)btn 
{ 
NSMutableArray *strArray = [[NSMutableArray alloc]init]; 
for (UIButton *btn in self.allBtnArrays) { 
if (btn.isSelected) { 
[strArray addObject:btn.currentTitle]; 
} 
} 
NSString *str = [strArray componentsJoinedByString:@" & "]; 
UIFont *font = [UIFont systemFontOfSize:14]; 
CGFloat strH = [str boundingRectWithSize:CGSizeMake(KscreenW-2*KViewMargin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.height; 
//展示文字 
if (!self.showLabel) { 
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(KViewMargin, self.sureButton.bottom+20, KscreenW-2*KViewMargin, strH)]; 
label.font = font; 
label.textColor = [UIColor redColor]; 
label.numberOfLines = 0; 
self.showLabel = label; 
[self.view addSubview:label]; 
} 
self.showLabel.text = str; 
self.showLabel.height = strH; 
} 
-(void)backBtnClick:(UIButton *)btn 
{ 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 
- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 
/* 
#pragma mark - Navigation 
// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 
@end

2:根據(jù)屏幕寬度變化。

//// StyleTwoViewController.m 
// buttonShow 
// 
// Created by limin on 16/11/15. 
// Copyright © 2016年 君安信(北京)科技有限公司. All rights reserved. 
// 
#import "StyleTwoViewController.h" 
#import "UIViewExt.h" 
#define kTagMargin 14 
#define kCellMargin 15 
#define kScreenWidth [UIScreen mainScreen].bounds.size.width 
#define Color(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0] 
@interface StyleTwoViewController () 
/* 按鈕 */ 
@property(nonatomic,strong)NSMutableArray *btnsArray; 
/* 按鈕文字 */ 
@property(nonatomic,strong)NSArray *tagsArray; 
/* 默認(rèn)選擇的按鈕 */ 
@property(nonatomic,strong)UIButton *selectedBtn; 
/* 標(biāo)簽 */ 
@property(nonatomic,copy)NSString *selectTopicTitle; 
@end 
@implementation StyleTwoViewController 
- (void)viewDidLoad { 
[super viewDidLoad]; 
self.view.backgroundColor = [UIColor whiteColor]; 
[self getTagMsg]; 
} 
-(void)getTagMsg 
{ 
_btnsArray = [NSMutableArray array]; 
//添加tag按鈕 
NSArray *tagsArray = @[@"美好生活1",@"美好生活2",@"美好生活3",@"美好生活4",@"美好生活5",@"美好生活6",@"美好生活7",@"美好生活8",@"美好生活9",@"美好生活10",@"美好生活11",@"美好生活12",@"美好生活13",@"美好生活14",@"美好生活15",@"美好生活16",@"美好生活17",@"美好生活18",@"美好生活19",@"美好生活20",@"美好生活21",@"美好生活22",@"美好生活23",@"美好生活24"]; 
_tagsArray = tagsArray; 
[self createTagSqures:tagsArray]; 
} 
#pragma mark - 創(chuàng)建方塊 
-(void)createTagSqures:(NSArray *)tags 
{ 
//一行最多4個。 
int maxCols = 4; 
//寬度、高度 
CGFloat TotalWidth = kScreenWidth - 2*kCellMargin - (maxCols-1)*kTagMargin; 
CGFloat BtnWidth = TotalWidth / maxCols; 
CGFloat BtnHeight = 30; 
for (int i=0; i<tags.count; i++) { 
//創(chuàng)建按鈕 
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
btn.backgroundColor = Color(211, 156, 4); 
//設(shè)置按鈕屬性 
[btn setBackgroundImage:[UIImage imageNamed:@"discover_btnbg"] forState:UIControlStateNormal]; 
[btn setBackgroundImage:[UIImage imageNamed:@"discover_btnbg"] forState:UIControlStateDisabled]; 
btn.adjustsImageWhenHighlighted = NO; 
[btn setTitleColor:Color(51, 51, 51) forState:UIControlStateNormal]; 
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled]; 
[btn.titleLabel setFont:[UIFont systemFontOfSize:13]]; 
btn.titleLabel.textAlignment = NSTextAlignmentCenter; 
btn.tag = i+10; 
// 監(jiān)聽點擊 
[btn addTarget:self action:@selector(TagBtnClick:) forControlEvents:UIControlEventTouchUpInside]; 
//按鈕賦值 
[btn setTitle:tags[i] forState:UIControlStateNormal]; 
[self.view addSubview:btn]; 
//計算frame 
int col = i % maxCols; 
int row = i / maxCols; 
btn.x = kCellMargin + col*(BtnWidth + kTagMargin); 
btn.y = 40 + 11 + row * (BtnHeight + kTagMargin); 
btn.width = BtnWidth; 
btn.height = BtnHeight; 
//設(shè)置所有按鈕默認(rèn)狀態(tài)為NO 。 
btn.enabled = YES; 
[self.btnsArray addObject:btn]; 
} 
//默認(rèn)第一個按鈕為選中 
UIButton *btn = self.btnsArray[0]; 
btn.enabled = NO; 
self.selectedBtn = btn; 
self.selectTopicTitle = self.tagsArray[0]; 
//返回按鈕 
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake((kScreenWidth-80)*0.5, self.view.height-160, 80, 80)]; 
[btn2 setTitle:@"返回" forState:UIControlStateNormal]; 
[btn2 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; 
btn2.layer.cornerRadius = 40; 
btn2.clipsToBounds = YES; 
btn2.layer.borderColor = [UIColor purpleColor].CGColor; 
btn2.layer.borderWidth = 2; 
[btn2 addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:btn2]; 
//創(chuàng)建確認(rèn)按鈕 
UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(kCellMargin, btn2.top-80, kScreenWidth-2*kCellMargin, 44)]; 
[btn3 setTitle:@"確認(rèn)" forState:UIControlStateNormal]; 
[btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
btn3.backgroundColor = Color(214, 116, 0); 
[btn3 addTarget:self action:@selector(showSelectedClick:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:btn3]; 
} 
#pragma mark - 按鈕點擊事件 
- (void)TagBtnClick:(UIButton *)button 
{ 
//獲取點擊的button,取消選中樣式 
self.selectedBtn.enabled = YES; 
button.enabled = NO; 
self.selectedBtn = button; 
NSInteger index = button.tag-10; 
NSString *selectStr = self.tagsArray[index]; 
self.selectTopicTitle = selectStr; 
} 
-(void)showSelectedClick:(UIButton *)button 
{ 
//保留選擇標(biāo)簽的文字 
NSLog(@"所選擇的文字:%@",self.selectTopicTitle); 
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:self.selectTopicTitle message:@"" delegate:nil cancelButtonTitle:@"確認(rèn)" otherButtonTitles:nil]; 
[alert show]; 
} 
-(void)backBtnClick:(UIButton *)btn 
{ 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 
- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 
/* 
#pragma mark - Navigation 
// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 
@end

以上所述是小編給大家介紹的iOS 中根據(jù)屏幕寬度自適應(yīng)分布按鈕的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • IOS開發(fā)之路--C語言基礎(chǔ)知識

    IOS開發(fā)之路--C語言基礎(chǔ)知識

    當(dāng)前移動開發(fā)的趨勢已經(jīng)勢不可擋,這個系列希望淺談一下個人對IOS開發(fā)的一些見解,今天我們從最基礎(chǔ)的C語言開始,C語言部分我將分成幾個章節(jié)去說,今天我們簡單看一下C的一些基礎(chǔ)知識,更高級的內(nèi)容我將放到后面的文章中。
    2014-08-08
  • 簡單說說iOS之WKWebView的用法小結(jié)

    簡單說說iOS之WKWebView的用法小結(jié)

    iOS8.0之后我們使用 WebKit框架中的WKWebView來加載網(wǎng)頁。這篇文章主要介紹了簡單說說iOS之WKWebView的用法小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • iOS實現(xiàn)翻頁效果動畫實例代碼

    iOS實現(xiàn)翻頁效果動畫實例代碼

    本篇文章主要介紹了iOS實現(xiàn)翻頁效果動畫實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • IOS實現(xiàn)輸入驗證碼、密碼按位分割(二)

    IOS實現(xiàn)輸入驗證碼、密碼按位分割(二)

    這篇文章主要介紹了IOS實現(xiàn)輸入驗證碼、密碼按位分割的方法,在App內(nèi),密碼及驗證碼的輸入,采用按位輸入的方法,且位與位之間有分隔線,感興趣的小伙伴們可以參考一下
    2016-01-01
  • iOS中使用RSA加密詳解

    iOS中使用RSA加密詳解

    本文主要介紹了iOS中使用RSA加密的方法,具有一定的參考價值,下面跟著小編一起來看下吧
    2016-12-12
  • iOS內(nèi)存錯誤EXC_BAD_ACCESS的解決方法

    iOS內(nèi)存錯誤EXC_BAD_ACCESS的解決方法

    iOS開發(fā),最郁悶的莫過于程序毫無征兆地就崩潰了,用bt命令打出調(diào)用棧,給出的是一堆系統(tǒng)EXC_BAD_ACCESS的信息,根本沒辦法定位問題出現(xiàn)在哪里
    2013-06-06
  • 如何去掉Xcode工程中某種類型的警告

    如何去掉Xcode工程中某種類型的警告

    這篇文章主要給大家介紹了關(guān)于如何去掉Xcode工程中某種類型的警告,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Xcode具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • iOS中NSInvocation的基本用法教程

    iOS中NSInvocation的基本用法教程

    NSInvocation是IOS消息傳遞和方法調(diào)用的一個類,下面這篇文章主要給大家介紹了關(guān)于iOS中NSInvocation的基本用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面隨著小編來一起看看吧。
    2017-09-09
  • iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析

    iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析

    這篇文章主要介紹了iOS 開發(fā)中 NavigationController經(jīng)常出現(xiàn)的問題原因分析的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 使用Reachability類判斷iOS設(shè)備的當(dāng)前網(wǎng)絡(luò)連接類型

    使用Reachability類判斷iOS設(shè)備的當(dāng)前網(wǎng)絡(luò)連接類型

    這篇文章主要介紹了使用Reachability類判斷iOS設(shè)備的當(dāng)前網(wǎng)絡(luò)連接類型,這里開發(fā)語言為傳統(tǒng)的Objectice-C,需要的朋友可以參考下
    2016-02-02

最新評論