一. 基本配置

  1 项目图标

    将图片直接拖入Assets-AppIcon

  2 启动图片

    

       

       

  3 软件名称

      

  4 删除Main.stroryboard

       

  5 设置窗口的根控制器

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2.  
  3. //创建一个窗口
  4. self.window = [[UIWindow alloc] init];
  5. self.window.frame = [UIScreen mainScreen].bounds;
  6.  
  7. //设置窗口的根控制器
  8. self.window.rootViewController = [[ViewController alloc] init];
  9.  
  10. //显示窗口
  11. [self.window makeKeyAndVisible];
  12.  
  13. return YES;
  14. }

二   配置TabBar切换控制器

  先把项目的骨架搭起来,即底部的TabBar控制器

  1 基本UITabBarController设置(将tabBar的基本设置封装到自己的tabBar中)

  1. UITabBarController *tabBarController = [[UITabBarController alloc] init];
  2.  
  3. UIViewController *vc1 = [[UIViewController alloc] init];
  4. vc1.view.backgroundColor = [UIColor redColor];
  5. vc1.tabBarItem.title = @"精华";
  6. vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
  7. vc1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
  8. [tabBarController addChildViewController:vc1];
  9.  
  10. UIViewController *vc2 = [[UIViewController alloc] init];
  11. vc2.view.backgroundColor = [UIColor grayColor];
  12. vc2.tabBarItem.title = @"新帖";
  13. vc2.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
  14. vc2.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];
  15. [tabBarController addChildViewController:vc2];
  16.  
  17. UIViewController *vc3 = [[UIViewController alloc] init];
  18. vc3.view.backgroundColor = [UIColor blueColor];
  19. vc3.tabBarItem.title = @"关注";
  20. vc3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
  21. vc3.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
  22. [tabBarController addChildViewController:vc3];
  23.  
  24. UIViewController *vc4 = [[UIViewController alloc] init];
  25. vc4.view.backgroundColor = [UIColor purpleColor];
  26. vc4.tabBarItem.title = @"我";
  27. vc4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
  28. vc4.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];
  29. [tabBarController addChildViewController:vc4];
  30.  
  31. //设置窗口的根控制器
  32. self.window.rootViewController = tabBarController; 

    

   2 选中图片会默认做一个自动渲染

  1. UIImage *image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
  2. //设置图片不会被渲染
  3. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

  3  设置字体  

  1. NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  2. attrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
  3. attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
  4. [vc1.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
  5.  
  6. NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
  7. selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
  8. selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
  9. [vc1.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

    

    

三 appearance的使用

  通过appearance统一设置所有UITabBarItem的属性,后面带有UI_APPEARANCE_SELECTOR的方法,都可以通过appearance对象来统一设置       

  1. NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  2. attrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
  3. attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
  4.  
  5. NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
  6. selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
  7. selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
  8.  
  9. UITabBarItem *item = [UITabBarItem appearance];
  10. [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
  11. [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

四 自定义子控制器

  将需要修改的属性作为变量,将代码的相同部分提取出来,可提高代码的可读性 

  在修改某些模块的时候,只需要在相应的子控制器中设置即可 

  1. /**
  2. * 初始化子控制器
  3. */
  4.  
  5. - (void)setupChildVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
  6. {
  7. //设置文字和图片
  8. vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform()/100.0 green:arc4random_uniform()/100.0 blue:arc4random_uniform()/100.0 alpha:];
  9. vc.tabBarItem.title = title;
  10. vc.tabBarItem.image = [UIImage imageNamed:image];
  11. vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
  12. //添加子控制器
  13. [self addChildViewController:vc];
  14. }
  1. [self setupChildVC:[[UIViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];

五 自定义tabbar

  1 直接在tabbar中添加项    

  1. UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
  2. [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
  3. [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
  4. publishButton.frame = CGRectMake(, , publishButton.currentBackgroundImage.size.width, publishButton.currentBackgroundImage.size.height);
  5. publishButton.center = CGPointMake(self.tabBar.frame.size.width / , self.tabBar.frame.size.height / );
  6. [self.tabBar addSubview:publishButton];

  2 拿到tabbar的子控件

  1. for (UIView * button in self.tabBar.subviews){
  2.  
  3. button.frame = CGRectMake(, , , );
  4. }

   

  3 当控制器的view布局完之后再布局一次

  1. - (void)viewDidLayoutSubviews{
  2.  
  3. for (UIView * button in self.tabBar.subviews){
  4.  
  5. button.frame = CGRectMake(, , , );
  6. }
  7. }

    

  4 当把子控件拿出来也无法改变属性时,需要自定义tabbar

   tabbar是只读属性,可以通过KVC,可以直接访问成员变量,自动赋值

  1. //更换tabbar
  2. [self setValue:[[BSTabBar alloc] init] forKey:@"tabBar"];

   已替换成功:

    

  5 重新布局子控件 - (void)layoutSubviews{

  1. [super layoutSubviews];
  2.  
  3. //设置发布按钮的frame
  4. self.publishButton.frame = CGRectMake(, , self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
  5. self.publishButton.center = CGPointMake(self.frame.size.width / , self.frame.size.height / );
  6.  
  7. //设置其他UITabBarButton的frame
  8.  
  9. CGFloat buttonY = ;
  10. CGFloat buttonW = self.frame.size.width / ;
  11. CGFloat buttonH = self.frame.size.height;
  12. NSInteger index = ;

  13. for (UIView * button in self.subviews) {
  14.  
  15. if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
  16.  
  17. CGFloat buttonX = buttonW * ((index > 1) ? (index + 1) : index);
  18. button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
  19.  
  20. index ++;
  21. }
  22. }

    

六 封装frame的修改

  1 使用分类

    在分类中声明@propery,只会生成方法的声明,不会生成方法的实现和带有_的成员变量

  1. @interface UIView (BSExtension)
  2.  
  3. @property (nonatomic, assign) CGFloat width;
  4. @property (nonatomic, assign) CGFloat height;
  5. @property (nonatomic, assign) CGFloat x;
  6. @property (nonatomic, assign) CGFloat y;
  7.  
  8. @end
  1. @implementation UIView (BSExtension)
  2.  
  3. - (void)setWidth:(CGFloat)width{
  4.  
  5. CGRect frame = self.frame;
  6. frame.size.width = width;
  7. self.frame = frame;
  8. }
  9.  
  10. - (void)setHeight:(CGFloat)height{
  11.  
  12. CGRect frame = self.frame;
  13. frame.size.height = height;
  14. self.frame = frame;
  15. }
  16.  
  17. - (void)setX:(CGFloat)x{
  18.  
  19. CGRect frame = self.frame;
  20. frame.origin.x = x;
  21. self.frame = frame;
  22. }
  23.  
  24. - (void)setY:(CGFloat)y{
  25.  
  26. CGRect frame = self.frame;
  27. frame.origin.y = y;
  28. self.frame = frame;
  29. }
  30.  
  31. - (CGFloat)width
  32. {
  33. return self.frame.size.width;
  34. }
  35.  
  36. - (CGFloat)height
  37. {
  38. return self.frame.size.height;
  39. }
  40.  
  41. - (CGFloat)x
  42. {
  43. return self.frame.origin.x;
  44. }
  45.  
  46. - (CGFloat)y
  47. {
  48. return self.frame.origin.y;
  49. }
  50. @end

  2 使用pch文件

    

七 设置导航栏

  1 包装一个导航栏控制器,添加导航栏控制器为tabbarcontroller的子控制器

  1. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  2. [self addChildViewController:nav];

  2 设置导航栏内容

  1. //设置导航栏内容
  2. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
  3.  
  4. UIButton *tagButton = [UIButton buttonWithType:UIButtonTypeCustom];
  5. [tagButton setBackgroundImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:UIControlStateNormal];
  6. [tagButton setBackgroundImage:[UIImage imageNamed:@"MainTagSubIconClick"] forState:UIControlStateHighlighted];
  7. tagButton.size = tagButton.currentBackgroundImage.size;
  8. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tagButton];

    

  3 使用自定义的NSLog,在调试的时候自动隐藏

  1. #ifdef DEBUG
  2. #define BSLog(...) NSLog(__VA_ARGS__)
  3.  
  4. #else
  5. #define BSLog(...)
  6. #endif

  4 打印当前调用的函数

  1. #define BSLogFun BSLog(@"%s",__func__)

  

八 封装UIBarButtonItem

  1 使用扩展类给UIBarButtonItem添加一个方法

  1. @interface UIBarButtonItem (BSExtension)
  2.  
  3. + (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;
  4.  
  5. @end
  1. @implementation UIBarButtonItem (BSExtension)
  2.  
  3. + (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action{
  4.  
  5. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  6. [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
  7. [button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
  8. button.size = button.currentBackgroundImage.size;
  9. [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  10.  
  11. return [[self alloc] initWithCustomView:button];
  12. }
  13.  
  14. @end
  1. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];

  

九 调整项目文件结构

  设置全局背景色 

  1. #define BSRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0];
  2. #define BSGlobalBg BSRGBColor(223, 223, 223) 

  

十 自定义导航栏控制器

  1 可以重写push方法来拦截所有push进来的控制器  

  1. /**
  2. * 可以在这个方法中拦截所有push进来的控制器
  3. */
  4. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  5. {
  6. [super pushViewController:viewController animated:animated];
  7. viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
  8. NSLog(@"%@",viewController);
  9. }

  2 改变导航栏的返回按钮的颜色(默认为黑, 点击为红)和位置

  1. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  2. {
  3.  
  4. if (self.childViewControllers.count > ) {//如果push进来的不是第一个控制器
  5.  
  6. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  7. [button setTitle:@"返回" forState:UIControlStateNormal];
  8. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  9. [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  10. [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
  11. [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
  12. button.size = CGSizeMake(, );
  13. //让按钮内部的所有内容左对齐
  14. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  15. //按钮的外边距
  16. button.contentEdgeInsets = UIEdgeInsetsMake(, -, , );
  17. [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
  18. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  19. }
  20.  
  21. //让viewController可以覆盖上面设置的leftBarButtonItem
  22. [super pushViewController:viewController animated:animated];
  23.  
  24. }   

  3 设置tabbar隐藏

  1. //隐藏tabbar
  2. viewController.hidesBottomBarWhenPushed = YES;

十一 调整初始化代码

   当第一次使用某个类时调用一次,在使用appearance时可以放入

  1. /**
  2. * 当第一次使用这个类时调用一次
  3. */
  4. + (void)initialize
  5. {
  6. //当导航栏用在BSNavigationController中,appearence设置才会生效
  7. UINavigationBar *bar = [UINavigationBar appearance];
  8. [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
  9. }

 

十二 颜色说明

  1 24bit颜色:  R G B

    ff0000: 红色

    00ff00: 绿色

      0000ff: 蓝色

000000: 黑色

      ffffff: 白色

    灰色特点:RGB一样

  2 32bit颜色:  R G B A

ff0000ff

十三 关注模块

  1 自动换行

    

  2 label文字换行

    alt+enter

    

十四 显示推荐关注

  1 添加第三方框架

    cd + 项目路径

    vim PodFile

    

    pod install

    

  2 发送请求 <AFNetworking>

  1. //发送请求
  2.  
  3. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  4. params[@"a"] = @"category";
  5. params[@"c"] = @"subscribe";
  6.  
  7. [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  8.  
  9. NSLog(@"%@",responseObject);
  10.  
  11. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  12.  
  13. }];

  3 显示指示器 <SVProgressHUD>    

  1. /** 简单的等待提示
  2.  
  3. 开始: [SVProgressHUD show];
  4. 结束: [SVProgressHUD dismiss];
  5. 提示内容: [SVProgressHUD showSuccessWithStatus:@"加载中..."];
  6.  
  7. */
  8.  
  9. /** 显示HUD以及指示任务的状态
  10.  
  11. + (void)show;
  12. + (void)showWithStatus:(NSString*)string;
  13.  
  14. */
  15.  
  16. /** 在HUD指示任务的进度
  17.  
  18. + (void)showProgress:(CGFloat)progress;
  19. + (void)showProgress:(CGFloat)progress status:(NSString*)status;
  20.  
  21. */
  22.  
  23. /** 隐藏HUD
  24.  
  25. + (void)dismiss;
  26. + (void)dismissWithDelay:(NSTimeInterval)delay;
  27.  
  28. */
  29.  
  30. /** 显示多个HUD 该HUD将自动消失,popActivity将与显示的次数匹配
  31.  
  32. + (void)popActivity;
  33.  
  34. */

BSBuDeJie_01的更多相关文章

随机推荐

  1. Mac Pro 资源管理器 Double Commander“个性化设置” 备份

    操作系统自带的资源管理器,总是有些别扭的地方,在 Windows 系统下,我一般用 Total Commander(破解版)来作为替代品.现在换为 Mac 系统,自带的 Finer 也不怎么好用,连最 ...

  2. eclipse for php现有项目不能导入问题

    1.少了.project文件 解决办法:创建一个新项目,然后将新项目文件夹下的.project文件复制到将要导入的文件夹中.

  3. jquery版小型婚礼(可动态添加祝福语)

    前两天在网上不小心看到“js许愿墙”这几个字,我的神经就全部被调动了.然后就开始我的百度生涯,一直寻觅许愿墙背景图片和便利贴图片,觅了好久……一直没找到满意的……无意间看到祝福语和一些卡通婚礼图片.最 ...

  4. Android 组件和进程的一些关系

    1.Service 如果没有单独指定线程的话,那么和主activity是在同一个线程的,就是主线程,那么也不能够在这个service中做耗时操作,否则一定会中断退出. 2.默认情况下,一个Androi ...

  5. ABAP 上传文件到内表

    http://www.cnblogs.com/jiangzhengjun/p/4265642.html http://www.xuebuyuan.com/1233478.html

  6. 开发ios的语言

    iOS发展这么多年了,很多第三方语言都向开发一种自己的iOS,于是多种跨平台诞生了! Object-c.swift: 当然是开发iOS的首先,毕竟是苹果自己的东西,也是最流行.最适合开发ios的,无论 ...

  7. quartz.net插件类库封装(含源码)

    1.前言 目录: 1.quartz.net任务调度:源码及使用文档 2.quartz.net插件类库封装 最近项目需要做一写任务作业调度的工作,最终选择了quartz.net这个插件,它提供了巨大的灵 ...

  8. MySql增加字段、删除字段、修改字段名称、修改字段类型

    1.增加一个字段 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空 alter table user a ...

  9. hdu 1358 Period

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路:Next数组的用法,在第i个位置上如果有i%(i-Next[i])==0的话最小循环节就是 ...

  10. AngularJS 表单

    AngularJS 表单是输入控件的集合. HTML控件 以下 HTML input 元素被称为 HTML 控件: input 元素 select 元素 button 元素 textarea 元素 H ...