ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

iOS开发之UI开发2(xib、storyboard、字典转模型、MVC)

2021-08-05 18:57:38  阅读:273  来源: 互联网

标签:控件 xib storyboard 10 self iOS lblMsg dict appView


添加控件

获取控制器管理的view的宽度

CGFloat viewWidth = self.view.frame.size.width;

控件的最大x和y值(x值+宽度、y值+高度)

获取其他控件的最大y值,就是下面控件的y值
CGFloat btnY = CGRectGetMaxY(lblName.frame);

view的嵌套

创建View
UIView *appView = [[UIView alloc]init];
appView.backgroundColor = [UIColor blueColor];
appView.frame = CGRectMake(10,10,100,100);
[self.view addSubview:appView];

View里添加image
UIImageView *imgViewIcon = [[UIImageView alloc]init];
imgViewIcon.backgroundColor = [UIColor yellowColor];
imgViewIcon.frame = CGRectMake(10,10,10,10);
[appView addSubview:imgViewIcon];


View里添加label
UILabel *lblName = [[UILabel alloc]init];
lblName.backgroundColor = [UIColor redColor];
lblName.frame = CGRectMake(10,20,10,10);
[appView addSubview:lblName];
标题
lblName.text = appDitt[@"name"];
字体大小
lblName.font = [UIFont systemFontOfSize:12];
居中
lblName.textAlignment = NSTextAlignmentCenter;


View里添加UIButton
UIButton *btnDownload = [[UIButton alloc]init];
btnDownload.backgroundColor = [UIColor greenColor];
btnDownload.frame = CGRectMake(10,30,10,10);
[appView addSubview:btnDownload];

[btnDownload setTitle:@"下载" forState:UIControlStateNormal];
[btnDownload setTitle:@"已安装" forState:UIControlStateDisabled];

[btnDownload setBackgroundImage:[UIImage imageNamed:@"buttongreen"]forState:UIControlStateNormal];
[btnDownload setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"]forState:UIControlStateDisabled];
字体大小
btnDownload.titleLabel.font = [UIFont systemFontOfSize:14];
单击事件
[btnDownload addTarget:self action:@selector(btnDownloadClick) forControlEvents:UIControlEventTouchUpInside];

xib

xib是轻量级界面描述文件,storyboard是重量级界面描述文件
storyboard还可以描述多个界面,以及不同界面之间的跳转关系,两个最终都是创建代码

  • 创建 -> IOS -> User Interface -> Empty文件
  • 通过动态加载xib文件创建里面的view

1.1找到根目录
NSBundle *rootBundle = [NSBundle mainBundle];
1.2找到应用根目录下的xib(nib)文件,不需要后缀
UIView *appView = [[rootBundle loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];//返回数组存储控件,需要first或者last取出要的控件
1.3设置appView的frame属性
appView.frame = CGRectMake(appX,appY,appW,appH);
1.4appview添加到self.view中
[self.view addSubview:appView];

1.5设置appview的子控件数据(方法一:耦合性高)
UIImageView *imgViewIcon = (UIImageView *)[appView viewWithTag:1000];
imgViewIcon.image = [UIImage imageNamed:appModel.icon];

UILable *lblName = (UILable *)[appView viewWithTag:2000];
UILable.text = appModel.name;

1.5设置appview的子控件数据(方法二)
xib文件中 CustomClass 选择一个自己创建的一个继承自UIView的类
xib中控件可以拖拽到该类进行绑定

imgViewIcon.imgViewIcon.image = [UIImage imageNamed:appModel.icon];
UILable.lblName.text = appModel.name;

1.5设置appview的子控件数据(方法三)
在方法二的基础之上,将其image和text封装
再加一个model属性,重写set方法,将model数据解析
appView.model = appModel;
该部分也可以封装进appView里面
+(instancetype)appView{
NSBundle *rootBundle = [NSBundle mainBundle];
return [[rootBundle loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];//返回数组存储控件,需要first或者last取出要的控件
}

shift + option + command + 左/右 折叠/展开代码

字典转模型

@interface CZApp:NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)appWithDict:(NSDictionary *)dict;
@end

@implementation CZApp
-(instancetype)initWithDict:(NSDictionary *)dict
{
if(self=[super init])
{self.name=dict[@"name"];self.icon=dict[@"icon"];}
return self;
}

+(instancetype)appWithDict:(NSDictionary *)dict
{return [[self alloc] initWithDict:dict];}
@end

主函数
NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrayModels = [NSMutableArray array];
方法一
for(NSDictionary *dict in arrayDict){
   CZApp *model = [[CZApp alloc] init];
   model.name = dict[@"name"];
   model.icon = dict[@"icon"];
   [arrayModels addObject:model];
}

方法二
for(NSDictionary *dict in arrayDict){
   //CZApp *model = [[CZApp alloc] initWithDict:dict];这个也可以
   CZApp *model = [CZApp appWithDict:dict];

   [arrayModels addObject:model];
}

label浮现和消失

UILabel *lblMsg = [[UILabel alloc] init];
lblMsg.text = @"正在下载...";
lblMsg.backgroundColor = [UIColor blackColor];
lblMsg.frame = CGRectMake(10,10,10,10);
lblMsg.textColor = [UIColor redColor];

居中
lblMsg.textAlignment = NSTextAlignmentCenter;
lblMsg.font = [UIFont boldSystemFontOfSize:17];

透明度,后面通过动画减少透明度
lblMsg.alpha = 0;

设置四个角的半径
lblMsg.layer.cornerRadius = 10;
把多余的角去掉
lblMsg.layer.masksToBounds = YES;
动画
[UIView animateWithDuration:2.0 animations:^{
lblMsg.alpha = 0.6;
} completion:^(BOOL finsished){
     if(finished){//执行完毕
     //开始新的动画 延迟1秒后执行     
     [UIView animateWithDuration:1.5 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
lblMsg.alpha = 0;
     } completion:^(BOOL finsished){
     if(finished){[lblMsg removeFromSuperview];}
     }]
     }
}]




[self.superview addSubview:lblMsg];

MVC

目录可以建立四个文件夹group:models、views、controllers、others

  • controllers:viewControllers
  • models:模型类
  • view:mainStoryboard,xib,具体的view
  • others:AppDelegate程序代理
  • supporting files:images.xcassets

改变状态栏的文字颜色

重写方法
-(UIStatusBarStyle)preferredStatusBarStyle
{
   return UIStatusBarStyleLightContent//改为白色
}
隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
   return YES;
}

控件显示至最上层

[self.view bringSubviewToFront:self.btnIcon];

清除view所有控件

每个子控件分别调用removeFromSuperview方法,内部执行循环
[self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

延迟

[self performSelector:@selector(方法) withObject:nil afterDelay:0.5];

设置图标和启动图片

同一张图片要做很多张:1.iphone屏幕大小和分辨率不同 2.不同地方显示尺寸不同

在这里插入图片描述

  • 开发使用的是点,ios系统自动转换为对应的像素,自动找图片
  • @2x 视网膜屏幕,在原来点坐标的大小上乘于2,@3x同理

设置图标

  • 选中images.xcassets -> AppIcon 拖进图标

启动图片

  • 选择项目 -> App Icon and Launch Images -> Launch Images Source -> Launchimage Launch Screen File删除内容
  • 选中images.xcassets -> Launchimage 拖进图标

标签:控件,xib,storyboard,10,self,iOS,lblMsg,dict,appView
来源: https://blog.csdn.net/weixin_46926959/article/details/118813917

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有