[純程式碼]Swift 2.0 開發新浪微博--第一天

來源:互聯網
上載者:User

標籤:swift2.0   xcode   ios   

開發環境:Xcode 7  Beta 4 


1.1: 部署初步架構


1.2 設定根控制器(AppDelegate.swift)

<span style="font-family:Microsoft YaHei;">   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        // Override point for customization after application launch.                window = UIWindow (frame: UIScreen.mainScreen().bounds )        window?.backgroundColor = UIColor.whiteColor()        window?.rootViewController = MainUITabBarController()        window?.makeKeyAndVisible()        return true    }</span>


1.3 自訂TarbBar

<span style="font-family:Microsoft YaHei;">import UIKitclass MainUITabBarController: UITabBarController {    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view.        addChildViewControllers()        setComposeButton()    }       ///設定Button    private func setComposeButton() {            //計算Button 高度        let w = tabBar.bounds.width / CGFloat (viewControllers!.count)                // Button 已有寬和高的Rect        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)                //使用位移 設定Button位置        composeButton.frame = CGRectOffset(rect, 2 * w, 0)            }         /// 添加所有子控制器    private  func addChildViewControllers() {                //Start添加所有子控制器--------------------------        addChildViewController(HomeTableViewController(), title:"首頁",imageName:"tabbar_home" )                addChildViewController(MessageTableViewController(), title:"訊息",imageName:"tabbar_message_center" )                addChildViewController(UIViewController())                addChildViewController(DiscoverTableViewController(), title:"發現",imageName:"tabbar_discover" )                addChildViewController(profileTableViewController(), title:"我",imageName:"tabbar_profile" )        //end-----------------------------------------    }        ///懶載入建立Button    lazy private var composeButton: UIButton = {                 let button = UIButton ()                //Start設定Button素材---------------------------------        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)                button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)                button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal )                button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted )        //end-----------------------------------------------                //此處不提示得手敲        self.tabBar.addSubview(button)                button.addTarget(self, action: "ClickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)                return button            }()            /// 添加控制器方法    ///    /// - parameter vc       : 視圖控制器    /// - parameter title    : 標題    /// - parameter imageName: 映像名稱    private func addChildViewController(vc:UIViewController, title: String, imageName: String) {        tabBar.tintColor = UIColor.orangeColor()        vc.title = title        vc.tabBarItem.image = UIImage(named: imageName)        let nav = UINavigationController(rootViewController: vc)        addChildViewController(nav)            }    </span>


1.4 通過繼承關係設定登入介面

注: 根據使用者是否登入來判斷介面是否切換

1.4.1自訂一個繼承UITableViewController的控制器


1.4.2 修改所有控制器的繼承關係,繼承BaseTableViewController


1.4.3 設定BaseTableViewController的View

<span style="font-family:Microsoft YaHei;">    ///設定自訂登入View    private func setVistorView() {            //建立空View,測試使用        view = UIView ()        //設定顏色        view.backgroundColor = UIColor.orangeColor()                    }</span>


1.4.4 重寫BaseTableViewController 的loadView()方法

<span style="font-family:Microsoft YaHei;">//定義一個使用者是否登入的開關    var userLogin = true</span>

<span style="font-family:Microsoft YaHei;">    override func loadView() {        //判斷使用者登入是否成功切換View        userLogin ? super.loadView() : setVistorView()          }</span>

連續12天更新 每天更新一階段

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

[純程式碼]Swift 2.0 開發新浪微博--第一天

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.