美文网首页社交类项目总结
创建程序的主架构

创建程序的主架构

作者: iOS_Cqlee | 来源:发表于2015-12-25 22:00 被阅读118次

Second Chapter

创建程序的主架构

  • 大多数一般都使用tabBarController嵌套着navgationCotroller,系统默认的tabBarItem如果不是我们需要的,要想修改,可以统一通过UIAppearance来设置.
  • 只要方法名或者属性名后面带有UI_APPEARANCE_SELECTOR这个宏,就可以通过[XXX appearance]对象来统一设置
//设置普通状态文字的状态
    
    NSMutableDictionary *normalAtts = [NSMutableDictionary dictionary];
    normalAtts[NSForegroundColorAttributeName] = [UIColor grayColor];
    normalAtts[NSFontAttributeName] = [UIFont systemFontOfSize:14];

    //设置选中状态文字的状态
    NSMutableDictionary *SelectedAtts = [NSMutableDictionary dictionary];
    SelectedAtts[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    SelectedAtts[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAtts forState:UIControlStateNormal];
    [item setTitleTextAttributes:SelectedAtts forState:UIControlStateSelected];
    

  • 添加子控制器

- (void)setupAllChildVc{
    
    [self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqEssenceViewController alloc] init]] title:@"" imageName:@"" selectedImage:@""];

    [self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqNewViewController alloc] init] ] title:@"" imageName:@"" selectedImage:@""];
    

    
    [self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqFocusViewController alloc] init] ] title:@"" imageName:@"" selectedImage:@""];
    
    
    
    [self setupChildVc:[[CqNavigationViewController alloc] initWithRootViewController:[[CqMeViewController alloc] init] ]title:@"我" imageName:@"" selectedImage:@""];
}



/**
 *  初始化一个子控制器
 *
 *  @param childVcClass  子控制器的具体类型
 *  @param title         子控制器的标题
 *  @param image         子控制器的图标
 *  @param selectedImage 子控制器的选中图标
 */
- (void)setupChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
//    childVc.view.backgroundColor = CqRandomColor;
    childVc.tabBarItem.title = title;
    if (imageName.length) {
        
        childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    }
    if (selectedImage) {
        
        childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    [self addChildViewController:childVc];
    
    
}

  • 如果系统的tabBar不能满足需求,可以自己创建自己的tabBar系统的tabBar,根据用户添加了多少自控制器来决定,点击切换,如果单纯在tabBar添加一个按钮,系统的tabBar就不足以让我们去使用了,这就需要我们自己去自定义了.

  • 为tabBar添加按钮有两种方法

  • 多添加一个自控制器,给后来添加的按钮留下位置,在添加一个按钮覆盖到添加空的子控制器留的tabBar位置上,这种方法可以实现我们的需求,但是,会浪费了一个控制器,直接创建出来什么,没有多大的用处.

  • 另外一种是重新布局tabBar内部的排列,一般tabBar是根据子控制的个数,将tabBar宽度平均分成自控制器的个数,我们可以通过修改每个自控制器在tabBar现实的位置进行重新分布,为了避免重复添加按钮,可以对按钮执行懒加载.

  • 例如自定义tabBar

- (UIButton *)publishBtn{
    if (!_publishBtn) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [self addSubview:btn];
        
        [btn addTarget:self action:@selector(publishBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _publishBtn = btn;
        
        
    }
    
    return _publishBtn;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    /** 按钮的尺寸 **/
    CGFloat tabButtonW = self.Cq_width * 0.2;
    CGFloat tabButtonH = self.Cq_height;
    
    /** 设置publishButton的frame **/
    self.publishBtn.Cq_width= tabButtonW;
    self.publishBtn.Cq_height = tabButtonH;
    self.publishBtn.center = CGPointMake(self.Cq_width * 0.5, self.Cq_height * 0.5);
    
    //用来记录的索引
    int tabBarIndex = 0;
  
    for (UIControl *tabBarButton in self.subviews) {
        //如果不是系统UItabBarButton的,跳过
        if (![@"UITabBarButton" isEqualToString:NSStringFromClass(tabBarButton.class)] ){
            continue;
        }
        //布局tabBar子控件的位置
        tabBarButton.Cq_width = tabButtonW;
        tabBarButton.Cq_height = tabButtonH;
        tabBarButton.Cq_y = 0;
        tabBarButton.Cq_x = tabBarIndex * tabButtonW;
        if (tabBarIndex >= 2) {
            tabBarButton.Cq_x += tabButtonW;
        }
        //记录点击的tabBarButton
        tabBarButton.tag = tabBarIndex;

        //增加索引
        tabBarIndex++;
        
        //为了之后监听tabBar点击每个页面的切换,
        [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - 监听点击

- (void)tabBarButtonClick:(UIControl *)tabBarButton{
    
    if (self.selectBtnIndex == tabBarButton.tag) {
        //发通知
        [[NSNotificationCenter defaultCenter] postNotificationName:CqTabBarButtonDidRepeatClickNotification object:nil];
    }
    
    //记录索引
    self.selectBtnIndex = tabBarButton.tag;
    
}

相关文章

  • 创建程序的主架构

    Second Chapter 创建程序的主架构 大多数一般都使用tabBarController嵌套着navgat...

  • 创建程序的主架构

    Third Chapter 创建程序的主架构 由于每次跳转返回的页面不同,如果不进行统一处理,有时候当前一页的控制...

  • 性能操作步骤

    ---创建脚本: (1)选择对应的协议; (2)应用程序类型:B/S架构选Internet应用程序;C/S架构选w...

  • 好程序员大数据培训分享:Storm架构

    好程序员大数据培训:Storm架构,Storm架构:master/slave 主节点:Nimbus 负责在集群上进...

  • ASP.NET MVC5基础 - MVC文件架构

    创建MVC项目 首先,我们使用Visual Studio2019创建一个MVC架构的应用程序(Visual Stu...

  • 第5章 识别架构特性

    识别架构特性是创建架构或确定现有架构有效性的第一步。为给定的问题或应用程序识别正确的架构特性(“ -ilities...

  • 程序员、技术主管和架构师

    程序员、技术主管和架构师 最近在进一步思考程序员的成长,曾经写过一篇《如何快速的成为架构师》,里面写了我对程序员主...

  • jaeger

    jaeger 架构 jaeger-client: 嵌入在应用程序里.负责span的创建以及上报 jaeger-ag...

  • Android的前期(一)学习——布局

    布局的创建——关于布局1.在Android程序中界面是通过布局文件设定的,在每个应用程序创建时都会默认包含一个主界...

  • Spark运行时的内核架构以及架构思考

    一: Spark内核架构 1,Drive是运行程序的时候有main方法,并且会创建SparkContext对象,是...

网友评论

    本文标题:创建程序的主架构

    本文链接:https://www.haomeiwen.com/subject/dyfbhttx.html