标题实在有够醒目的了
这几天开始在做实际的项目,也参考了好多框架,但都过于复杂,代码看得头晕,还是简书上的XMTV(熊猫TV的作者:http://www.jianshu.com/u/66a861134217)
弄的框架一目了然,而且代码逻辑清晰,于是单独把框架移过来到自己项目使用了,在此,贴上代码,供新手们观看,我也是借花献佛!
先来两张图
//
// Common.swift
// AppFrameworks
//
// Created by apiapia on 3/8/17.
// Copyright © 2017 apiapia. All rights reserved.
//
import Foundation
import UIKit
/// 归档路径
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
let SELECTED_CHANNELS: String = "selectedChannels.archive" // 选择频道列表
let UNSELECTED_CHANNELS: String = "unselectedChannels.archive" // 未选择频道列表
let PAGE_TITLES: String = "pagetitles.archive" // 保存的pagetitles
let HOME_CHILDVCS: String = "childvcs" // 首页contentView中的子控制器
let DEFAULT_CHILDVCS: String = "default" // 首页初始化的子控制器
let ALL_GMES: String = "GameVC.archive"
/// notification
let NotifyUpdateCategory = NSNotification.Name(rawValue:"notifyUpdateCategory")
let KSelectedChannel: String = "selectedChannel"
/// 常用属性
let kItemMargin : CGFloat = 10
let kHeaderViewH : CGFloat = 50
let kNormalItemW = (kScreenW - 3 * kItemMargin) / 2
let kNormalItemH = kNormalItemW * 3 / 4
let NormalCellID = "NormalCellID"
let SearchCellID = "SearchCellID"
let HeaderViewID = "HeaderViewID"
let kStatusBarH: CGFloat = 20
let kNavigationBarH: CGFloat = 44
let kTabBarH: CGFloat = 49
let kScreenW = UIScreen.main.bounds.width
let kScreenH = UIScreen.main.bounds.height
let BGCOLOR: UIColor = UIColor(gray: 244)
/// 初始化common
let common = Common()
class Common: NSObject {
// MARK: - 创建一个barButtonItem
class func itemWithImage(_ image:UIImage,highlightImage:UIImage,target:UIViewController,action:Selector) -> UIBarButtonItem{
let button = UIButton.init()
button.setBackgroundImage(image, for: UIControlState())
button.setBackgroundImage(highlightImage, for: .highlighted)
button.sizeToFit()
button.addTarget(target, action: action, for: .touchUpInside)
return UIBarButtonItem.init(customView: button)
}
// MARK: -解档归档(保存的是GameModel数组)
// 归档
func archiveData(channel: [GameModel], appendPath: String) {
let filePath = path.appendingPathComponent(appendPath)
NSKeyedArchiver.archiveRootObject(channel, toFile: filePath)
}
//反归档
func unarchiveData(appendPath: String) -> ([GameModel]?) {
let filePath = path.appendingPathComponent(appendPath)
return NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [GameModel]
}
// MARK: -解档归档(保存的是String数组)
// 归档
func archiveWithStringArray(channel: [String], appendPath: String) {
let filePath = path.appendingPathComponent(appendPath)
NSKeyedArchiver.archiveRootObject(channel, toFile: filePath)
}
//反归档
func unarchiveToStringArray(appendPath: String) -> ([String]?) {
let filePath = path.appendingPathComponent(appendPath)
return NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [String]
}
///
func archive(array: [AnyObject], appendPath: String) {
let filePath = path.appendingPathComponent(appendPath)
NSKeyedArchiver.archiveRootObject(array, toFile: filePath)
}
func unarchive(appendPath: String) -> ([AnyObject]?) {
let filePath = path.appendingPathComponent(appendPath)
return NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as! ([AnyObject]?)
}
}
//
// UIColor+Extension.swift
// AppFrameworks
//
// Created by apiapia on 3/8/17.
// Copyright © 2017 apiapia. All rights reserved.
//
import UIKit
extension UIColor {
/// rgb颜色
convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
self.init(red: r/255.0 ,green: g/255.0 ,blue: b/255.0 ,alpha:1.0)
}
/// 纯色(用于灰色)
convenience init(gray: CGFloat) {
self.init(red: gray/255.0 ,green: gray/255.0 ,blue: gray/255.0 ,alpha:1.0)
}
class func randomColor() -> UIColor {
return UIColor(r: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256)))
}
}
//
// AppDelegate.swift
// AppFrameworks
//
// Created by apiapia on 3/8/17.
// Copyright © 2017 apiapia. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow.init()
let tabBarController = TabBarController.init()
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
// 显示引导页
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
//
// TabBarController.swift
// AppFrameworks
//
// Created by apiapia on 3/8/17.
// Copyright © 2017 apiapia. All rights reserved.
//
import UIKit
class TabBarController: UITabBarController {
override class func initialize() {
var attrs = [String: NSObject]()
attrs[NSForegroundColorAttributeName] = UIColor(r: 87, g: 206, b: 138)
// 设置tabBar字体颜色
UITabBarItem.appearance().setTitleTextAttributes(attrs, for:.selected)
}
// MARK: - life cycle
override func viewDidLoad() {
super.viewDidLoad()
addAllChildViewControllers()
let backView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 49))
backView.backgroundColor = UIColor.white
tabBar.insertSubview(backView, at: 0)
tabBar.isOpaque = true
}
// MARK: - private method
/// 添加所有子控制器
func addAllChildViewControllers() {
setupOneChildViewController("首页", image: "menu_homepage_nor", selectedImage: "menu_homepage_sel", controller: HomeVC.init())
setupOneChildViewController("游戏", image: "menu_youxi_nor", selectedImage: "menu_youxi_sel", controller: GameVC.init())
setupOneChildViewController("娱乐", image: "menu_yule_nor", selectedImage: "menu_yule_sel", controller: EntertainmentVC.init())
setupOneChildViewController("小葱秀", image: "menu_goddess_nor", selectedImage: "menu_goddess_sel", controller: SmallShowVC.init())
// 记得删除掉Info.plist Main storyboard file base name
setupOneChildViewController("我的", image: "menu_mine_nor", selectedImage: "menu_mine_sel", controller: UIStoryboard(name: "MySB", bundle: nil).instantiateInitialViewController()!)
}
/// 添加一个子控制器
fileprivate func setupOneChildViewController(_ title: String, image: String, selectedImage: String, controller: UIViewController) {
controller.tabBarItem.title = title
controller.title = title
controller.view.backgroundColor = BGCOLOR
controller.tabBarItem.image = UIImage(named: image)
controller.tabBarItem.selectedImage = UIImage(named: selectedImage)
let naviController = NavigationController.init(rootViewController: controller)
self.addChildViewController(naviController)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// NavigationController.swift
// AppFrameworks
//
// Created by apiapia on 3/8/17.
// Copyright © 2017 apiapia. All rights reserved.
//
import UIKit
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// UINavigationBar.appearance().barTintColor = UIColor.white
// 设置naviBar背景图片
UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigationbarBackgroundWhite"), for: UIBarMetrics.default)
// 设置title的字体
// UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 20)]
self.interactivePopGestureRecognizer?.delegate = nil
}
// MARK: - 拦截push控制器
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
print("拦截push控制器",self.viewControllers.count)
if self.viewControllers.count < 1 {
viewController.navigationItem.rightBarButtonItem = setRightButton()
} else {
viewController.hidesBottomBarWhenPushed = true
viewController.navigationItem.leftBarButtonItem = setBackBarButtonItem()
}
super.pushViewController(viewController, animated: true)
}
// MARK: - private method
func setBackBarButtonItem() -> UIBarButtonItem {
let backButton = UIButton.init(type: .custom)
backButton.setImage(UIImage(named: "setting_back"), for: .normal)
backButton.sizeToFit()
backButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
backButton.addTarget(self, action: #selector(NavigationController.backClick), for: .touchUpInside)
return UIBarButtonItem.init(customView: backButton)
}
/// 设置导航栏右边按钮
func setRightButton() -> UIBarButtonItem {
let searchItem = UIButton.init(type: .custom)
searchItem.setImage(UIImage(named: "searchbutton_nor"), for: .normal)
searchItem.sizeToFit()
searchItem.frame.size = CGSize(width: 30, height: 30)
searchItem.contentHorizontalAlignment = .right
searchItem.addTarget(self, action: #selector(NavigationController.searchClick), for: .touchUpInside)
return UIBarButtonItem.init(customView: searchItem)
}
/// 点击右边的搜索
func searchClick() {
// let searchvc = SearchVC()
// self.pushViewController(searchvc, animated: true)
}
/// 返回
func backClick() {
self.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
以上就是框架的全部,简单易懂,可以直接COPY,好爽!
还有一点,就是记得Info.plist 那个 Main storyboard 记得 delete掉哦!