我是个比较喜欢用storyboard和xib的人。我个人的习惯就是,能用storyboard的一定不用代码手工撸。当然自己业余个人写的项目,基本上一个storyboard就搞定了。但涉及到多人合作下时候,一个storyboard还是挺蛋疼的,冲突难解决,打开storyboard极容易出现修改。结构大的时候打开还卡(也可能是我电脑太屌丝啦。。。)

下面介绍下我使用多个storyboard的习惯,仅供参看,不一定是最好的方案。

一、storyboard结构

  1. 默认的Main.storyboard下我只有一个UITabBarController,如果你建立的是TabItem的模板项目,这个应该是默认的。注意,没有tabItem。
  2. 给对应的每个TabBarItem建立对应的storyboard,比如我建立的MyTabItemOne.storyboard和MyTabItemTwo.storyboard。
  3. MyTabItemOne.storyboard下我放的是一个UINavigationController作为初始Controller。
  4. MyTabItemTwo.storyboard类似

二、代码层面

自定义UITabBarController和UINavigationController。将Main.storyboard下的UITabBarController改为自定义的SPTabBarController.swift。将MyTabItemOne(Two).storyboard下的UINavigationController改为自定义的SPNavigationController.swift

SPTabBarController.swift

//
//  SPTabBarController.swift
//  SampleProject
//
//  Created by LiuYanghui on 2016/11/14.
//  Copyright © 2016年 LiuYanghui. All rights reserved.
//

import UIKit

class SPTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // 添加所有子控制器
        addChildViewControllers()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - private method

    /// 添加所有子控制器
    private func addChildViewControllers() {
        setChildrenController(title: "One", image: UIImage(), selectedImage: UIImage(), storyboard: UIStoryboard(name: "MyTabItemOne", bundle: nil))

        setChildrenController(title: "Two", image: UIImage(), selectedImage: UIImage(), storyboard: UIStoryboard(name: "MyTabItemTwo", bundle: nil))

    }

    /// 添加一个子控制器
    private func setChildrenController(title:String, image:UIImage,selectedImage:UIImage, storyboard:UIStoryboard) {
        let controller = storyboard.instantiateInitialViewController()!
        controller.tabBarItem.title = title
        controller.tabBarItem.image = image
        controller.tabBarItem.selectedImage = selectedImage
        addChildViewController(controller)
    }
}

SPNavigationController.swift

//
//  SPNavigationController.swift
//  SampleProject
//
//  Created by LiuYanghui on 2016/11/14.
//  Copyright © 2016年 LiuYanghui. All rights reserved.
//

import UIKit

class SPNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.interactivePopGestureRecognizer?.delegate = nil
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if viewControllers.count > 0 {
            //隐藏tabbar
            viewController.hidesBottomBarWhenPushed = true

            viewController.navigationItem.leftBarButtonItem = setBackBarButtonItem()
        }
        super.pushViewController(viewController, animated: animated)
    }

    func back(){
        self.popViewController(animated: true)
    }

    // MARK: - Private method
    private func setBackBarButtonItem() -> UIBarButtonItem{
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "navigationButtonReturn"), for: .normal)
        button.setImage(UIImage(named: "navigationButtonReturnClick"), for: .highlighted)
        button.setTitle("返回", for: .normal)
        button.setTitleColor(UIColor(red: 81/255, green: 81/255, blue: 81/255, alpha: 1), for: .normal)
        button.addTarget(self, action: #selector(SPNavigationController.back), for: .touchUpInside)
        button.frame.size = CGSize(width: 100, height: 30)
        // button 里的内容左对齐
        button.contentHorizontalAlignment = .left
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)

        return UIBarButtonItem(customView: button)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Xcode工程

Sample Project Github

Swift下多个Storyboard的项目结构的更多相关文章

  1. Swift下自定义xib添加到Storyboard

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51657154 ...

  2. Maven下Java、JavaWeb约定标准项目结构

    1.Maven Java 项目结构: 2.Maven JavaWeb 项目结构: 注意:webapp下必须要有WEB-INF文件夹,WEB-INF文件夹下必须要有web.xml 跟 classes文件 ...

  3. 认识ASP.NET 5项目结构和项目文件xproj

    ASP.NET 5 在项目结构上做了很大的改变,我们以前熟悉的目录结构与项目文件内容都不太一样了,本篇文章带大家了解 ASP.NET 5 到底跟以前有哪些不一样的地方. 我们先用 Visual Stu ...

  4. 【完全开源】知乎日报UWP版:项目结构说明、关键源代码解释

    目录 说明 项目结构 关键代码 演示视频 说明 上一篇博客将源码放出来了,但是并没有做过多的介绍,所以如果自己硬看可能需要花费很长的时间,尤其这些代码并不是自己写的.项目不算复杂但是也不算简单,这篇文 ...

  5. Tomcat服务器本地的搭建,以及在 IDEA软件下的配置,以及项目的测试运行(基于supermvc框架下的web)

    一.声明 使用了基于springmvc的supermvc的web框架.实习公司的框架. 二.tomact的下载与安装 1选择适合自己电脑配置的jdk和jre版本(截图来自tomcat的官方网站http ...

  6. Mybatis 复习 Mybatis 配置 Mybatis项目结构

    pom.xml文件已经贴在了文末.该项目不使用mybatis的mybatis-generator-core,而是手写Entities类,DaoImpl类,CoreMapper类 其中,Entities ...

  7. vue.js在windows本地下搭建环境和创建项目

    Vue.js是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合.另一方面,Vu ...

  8. django开发个人简易Blog——构建项目结构

    开发之前第一步,就是构造整个的项目结构.这就好比作一幅画,第一步就是描绘轮廓,有了轮廓,剩下的就是慢慢的填充细节.项目结构规划如下图: 项目结构描述: 本项目以fengzhengBlog为根目录. a ...

  9. 架构系列:ASP.NET 项目结构搭建

    我们头开始,从简单的单项目解决方案,逐步添加业务逻辑的约束,从应用逻辑和领域逻辑两方面考虑,从简单的单个项目逐步搭建一个多项目的解决方案.主要内容:(1)搭建应用逻辑和领域逻辑都简单的单项目 (2)为 ...

随机推荐

  1. [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

  2. 【Swift】IOS开发中自定义转场动画

    在IOS开发中,我们model另外一个控制器的时候,一般都使用默认的转场动画. 其实我们可以自定义一些转场动画.达到不同的转场效果. 步骤如下:(photoBrowser是目标控制器) 1.在源控制器 ...

  3. [SCOI 2016]美味

    Description 题库链接 给你一个长度为 \(n\) 的序列 \(A\) . \(m\) 组询问 \((b,x,l,r)\) 询问 \[\max_{i=l}^r b\oplus (A_i+x) ...

  4. 为了异常安全(swap,share_ptr)——Effecive C++

    互斥锁: 假设我们要在多线程中实现背景图片的控制: class PrettyMenu{ public: -- void changeBackground(std::istream& imgSr ...

  5. bzoj3930[CQOI2015]选数 容斥原理

    3930: [CQOI2015]选数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1383  Solved: 669[Submit][Status] ...

  6. 在右键中添加以管理员运行CMD命令提示符 (进化版)

    直接代码,转过来的 20180316更新添加快捷键A,点右键按A即可: Windows Registry Editor Version 5.00 ; Created by: Shawn Brink ; ...

  7. C语言程序设计第一次作业 冯亚杰

    (一)实验总结 求圆面积和周长 (1)题目 输入圆的半径,计算圆的周长和面积. (2)流程图 (3)测试数据及运行结果 测试数据1:r=7 运行结果: 测试数据2:r=5 运行结果: (4)实验分析 ...

  8. Linux学习之CentOS(二)--初识linux的一些常用命令(基础命令)

    初次学习linux系统,首先也得会一些linux的基本命令.至少要先学会开启和关闭系统吧!我称为 基础命令! linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器. ...

  9. Mysql bug: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.

    在 MySQL 中执行命令试下: set global time_zone='+8:00': 解释:在访问数据库时出现时区无法识别问题,在通过在数据库连接URL后,加上?serverTimezone= ...

  10. java对redis的操作

    需要两个包的支持 jedis-2.1.0.jar commons-pool-1.5.4.jar 一个连接池一个工具类 pool代码 public class RedisUtil { private s ...