Swift 項目 01

來源:互聯網
上載者:User

標籤:

1.項目的搭建

  1.1 建立檔案

    1.1.1 刪除模板檔案 --> viewController.Swift, main.storyBoard

    1.1.2 建立項目結構, 主目錄 Classess --> 二級目錄Module(功能模組), Model(業務模型), Tools(工具)---> Module 的子目錄Main,Home,            Message,Discover,profile

    1.1.3 建立專案檔,建立每個功能模組對應的storyBoard+控制器,(每個視圖控制器都繼承自tabViewController)

      * 每個storyBoard,都是有navigationController + tabViewController構成  

      * navigationController  是初始控制器,tabViewController 分別繼承自對應的視圖控制器

      * 表格的可重用標示符 分別是 目錄名+Cell

      * 搭建完成的檔案目錄 

 

  1.2 添加子控制器

    1.2.1 由於是採用的是多控制器管理,所以採用代碼的方式來添加子控制器

    1.2.2 拖入素材,更改圖片的填充模式為原始圖片大小

    1.2.3 代碼的實現

/// 添加子控制器private func addChildViewControllers() {    tabBar.tintColor = UIColor.orangeColor()    addChildViewController("Home", "首頁", "tabbar_home")    addChildViewController("Message", "訊息", "tabbar_message_center")    addChildViewController("Discover", "發現", "tabbar_discover")    addChildViewController("Profile", "我", "tabbar_profile")}/// 添加子控制器/// @param sbName       storyboard 名稱/// @param title        標題/// @param imageName    圖片名稱private func addChildViewController(sbName: String, _ title: String, _ imageName: String) {    let sb = UIStoryboard(name: sbName, bundle: nil)    let nav = sb.instantiateInitialViewController() as! UINavigationController    nav.title = title    nav.topViewController.title = title    nav.tabBarItem.image = UIImage(named: imageName)    nav.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")    addChildViewController(nav)}

  

  1.3 自訂TabBar

    1.3.1 在4個控制器切換按鈕中,添加 撰寫按鈕, 自訂tabBar

    1.3.2 建立mainTabBar.Swift 檔案,修改storyBoard中tabBar的類

    1.3.3 按鈕的懶載入

/// 撰寫按鈕   lazy var composeButton: UIButton = {    let button = UIButton()    button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)    button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)    button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)    button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)    self.addSubview(button)    return button}()

    1.3.4 調整按鈕位置

override func layoutSubviews() {    super.layoutSubviews()    var index = 0    let w = self.bounds.size.width / CGFloat(buttonCount)    let h = self.bounds.size.height    for view in self.subviews as! [UIView] {        // 判斷子檢視類型        if (view is UIControl && !(view is UIButton)) {            view.frame = CGRectMake(CGFloat(index) * w, 0, w, h)
       if index += index == 1? 2 :1 } } self.composeButton.frame = CGRectMake(0, 0, w, h) self.composeButton.center = CGPointMake(self.center.x, h * 0.5)}

    1.3.5 給按鈕添加事件

 // 設定撰寫按鈕點擊函數    mainTabBar.composeButton .addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)

    1.3.6 用private 來修飾函數私人, 點擊事件不能修飾,因為系統監聽到點擊事件時,會向控制器發送點擊按鈕的訊息。

private func addChildViewController(sbName: String, _ title: String, _ imageName: String)

  1.4 總結

    * Swift 文法更加簡潔,字串拼接可以直接用“+”號,可是使用“_”來忽略不需要考慮的參數,類型校正更加的嚴格。

    * Swift 懶載入的寫法

    * 私人方法可以用private 來修飾

  

Swift 項目 01

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.