IOS开发入门iOS 根据屏幕宽度自适应分布按钮
白羽 2019-05-21 来源 :网络 阅读 443 评论 0

摘要:本文将带你了解IOS开发入门iOS 根据屏幕宽度自适应分布按钮,希望本文对大家学IOS有所帮助。

    本文将带你了解IOS开发入门iOS 根据屏幕宽度自适应分布按钮,希望本文对大家学IOS有所帮助。



   屏幕摆放的控件有两种方式,一种根据具体内容变化,一种根据屏幕宽度变化。
   下面我分别将两个方式,用代码的方式呈现:
   1:根据具体内容变化

   
  ////   StyleOneViewController.m//  buttonShow////  Created by limin on  15/06/15.//  Copyright © 2015年 信诺汇通信息科技(北京)有限公司. All rights  reserved.// #import "StyleOneViewController.h"#import  "UIViewExt.h"//每列间隔#define KViewMargin 10//每行列数高#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;/** 服务器提供按钮标签  */@property(nonatomic,strong)NSArray *tagInfoArray;//-------展示选中的文字/* 确认按钮  */@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":@"环境不错"},                          @{@"id":@"1",@"tagmsg":@"性价比高"},                          @{@"id":@"1",@"tagmsg":@"位置好找"},                          @{@"id":@"1",@"tagmsg":@"上菜快"},                          @{@"id":@"1",@"tagmsg":@"菜量足"},                          @{@"id":@"1",@"tagmsg":@"好吃"},                          @{@"id":@"1",@"tagmsg":@"态度好,服务周到"}                          ];         //挨个赋值    for  (int i=0; i<_tagInfoArray.count; i++)  {        NSDictionary *dict =  _tagInfoArray[i];        [self.btnIDArrays  addObject:dict[@"id"]];        [self.btnMsgArrays  addObject:dict[@"tagmsg"]];    }    [self  createBtns];}//创建按钮-(void)createBtns{    //创建放置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];    /**     *   数组存放适配屏幕大小的每行按钮的个数     */    NSMutableArray  *indexbtns=[self  returnBtnsForRowAndCol];    //统计按钮View的高度    btnViewHeight=indexbtns.count*(KVieH+KViewMargin)+10;         //设置btnView的高度    self.btnsView.height=btnViewHeight;         NSInteger  count=0;    CGFloat  Y;         //循环创建按钮    for  (int row=0; rowKscreenW-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:根据屏幕宽度变化。
   
    
   xxx有限公司. 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;/* 默认选择的按钮  */@property(nonatomic,strong)UIButton *selectedBtn;/* 标签  */@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 - 创建方块-(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   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程