iOS項目框架的搭建


好久沒寫博客了,近來學習swift,准備用swift仿寫個項目,就找了找appstore,找了一個叫半糖的項目,看着界面真不錯,但是感覺技術跟不上,先試着寫寫吧

檔成文件夾,如圖所示

打開文件夾,找到payload,打開,然后右擊顯示包內容,然后你就看到一大堆的資源文件了,不過你會發現找來找去都找不到tabbar的圖片,今天給大家介紹個厲害的工具https://github.com/devcxm/iOS-Images-Extractor,上Git搜索下

你會有意想不到的驚喜哦,下載下來后如圖,直接運行.xcworkspace文件

 

運行后如圖,直接將ipa文件拖進去,然后點擊start,完成后點擊Qutput Dir就能看到所有的資源了,是不是很棒!

好了,准備工作做完了,現在我們開始搭建項目,打開xode,新建項目,選擇swift

然后將適配目標定在7.0,不能橫豎屏

 

接着在資源文件里尋找icon,和lanuchImage,icon就找到3張,我就放了2張,記得按尺寸放啊,在Assets.xcassets文件里新建一個LaunchImage,還要記得更改general里設置

再將找到的資源文件中的lanuchImage按尺寸放入

 

運行結果,是不是很棒,圖標也變過來了

接下來,進行項目的分類搭建,這次我准備純代碼編寫,所以現將storyboard文件刪除,系統自動生成的ViewController也刪除,修改general里的設置

在項目文件夾下新建文件夾,這樣便於項目管理,因為如果在項目里直接newgroup,這個new出來的group是虛的,不便於管理

這是我的分類,在5個tab文件夾下,還有3個文件夾,model,view,controller文件夾,采用MVC模式

項目結構

我看了下,這個項目沒有引導圖的,所以先將tabbar的資源導入,記得一定要放在@2x的位置上,我放在第一個位置上,坑死我了,如圖

繼承UITabbarController創建BTTabController,並創建5個tab的controller,如圖

BTTabController的代碼如下,因為只有圖片,沒有文字,所以要設置tab的imageSet屬性

import UIKit

class BTTabController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        bulidView()
        
    }
    
    private func bulidView(){
        let imageArray = ["tab_首頁","tab_社區","tab_publish_add","tab_分類","tab_我的"]
        let selectImageArray = ["tab_首頁_pressed","tab_社區_pressed","tab_publish_add_pressed","tab_分類_pressed","tab_我的_pressed"]
        let viewNameArray = [HomeViewController(),CommunityViewController(),AddViewController(),ClassifyViewController(),MineViewController()]
        for i in 0..<imageArray.count{
            let ctrl = viewNameArray[i]
            let tab = UITabBarItem(title:nil, image:UIImage(named:imageArray[i])?.imageWithRenderingMode(.AlwaysOriginal), selectedImage:UIImage(named:selectImageArray[i])?.imageWithRenderingMode(.AlwaysOriginal))
            tab.imageInsets = UIEdgeInsets(top:5, left:0, bottom: -5, right: 0)
            ctrl.tabBarItem = tab
            let nav = UINavigationController(rootViewController:ctrl)
            addChildViewController(nav)
        }
    }
    
    
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

Appdelegata代碼,還有Common文件夾下

import UIKit

public let ScreenWidth: CGFloat = UIScreen.mainScreen().bounds.size.width
public let ScreenHeight: CGFloat = UIScreen.mainScreen().bounds.size.height
public let ScreenBounds: CGRect = UIScreen.mainScreen().bounds

 

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window?.backgroundColor = UIColor.whiteColor()
        window = UIWindow(frame: ScreenBounds)
        window!.makeKeyAndVisible()
        
        //設置全局導航欄的狀態
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 228/255, green: 57/255, blue: 65/255, alpha:1.0)
        
        bulidMainController()
        return true
    }

    private func bulidMainController(){
        
        window?.rootViewController = BTTabController()
    }

    
    func applicationWillResignActive(application: UIApplication) {
        
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
    }

    func applicationWillEnterForeground(application: UIApplication) {
        
    }

    func applicationDidBecomeActive(application: UIApplication) {
        
    }

    func applicationWillTerminate(application: UIApplication) {
        
    }
}

修改狀態欄顏色

結束運行效果如下

perfect,完成了最開始的一部分后面只需要在5個tab里完成代碼就行了,今天就寫到這里了啊,有點累啊.....,明天試着寫第一個界面,看了一下,相當復雜,估計要GG


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM