iOS ChildViewController使用樣本

來源:互聯網
上載者:User

標籤:

storyboard:

 

ViewController代碼:

////  ViewController.swift//  TestAddChildViewController////  Created by Fran on 16/2/18.//  Copyright © 2016年 kimree. All rights reserved.//import UIKitclass ViewController: UIViewController {    @IBOutlet weak var scrollMenu: UIScrollView!            let colorArray = [UIColor.whiteColor(), UIColor.redColor(), UIColor.grayColor(), UIColor.yellowColor(), UIColor.blueColor()]    var buttonVCMapping: [UIButton: UIViewController] = [:]    var currentVC: UIViewController!        override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                // 如果不加這個 有navigationController的時候 scrollView的contentOffset會自動去變化適應        self.automaticallyAdjustsScrollViewInsets = false                scrollMenu.backgroundColor = UIColor.redColor()                self.navigationController?.navigationBar.backgroundColor = UIColor.redColor()                addScrollMenuButtons()    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        override func viewDidAppear(animated: Bool) {        super.viewDidAppear(animated)    }        // 添加scrollView上面的button    func addScrollMenuButtons(){        // scrollMenu height = 50 已知                for var i = 0; i < 5; i++ {            let button = UIButton(type: UIButtonType.Custom)            button.setTitle("button\(i)", forState: UIControlState.Normal)            button.frame = CGRectMake(CGFloat(i * 100) + 10, 5, 90, 40)            scrollMenu.addSubview(button)                        // 記錄button與對應要顯示的viewcontroller之間的映射            addButtonVCMapping(button, index: i)                        // button點擊事件            button.addTarget(self, action: "menuButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)        }                scrollMenu.contentSize = CGSizeMake(CGFloat(5 * 100) + 10, 49)    }        // button and viewcontroller mapping    func addButtonVCMapping(button: UIButton, index: Int){        let vc = UIViewController()        vc.view.frame = CGRectMake(0, 64 + 50, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 64 - 50)        vc.view.backgroundColor = colorArray[index]                buttonVCMapping[button] = vc        self.addChildViewController(vc)                // 設定初始時顯示哪一個childViewController        if index == 0{            self.view.addSubview(vc.view)            currentVC = vc        }    }        // 點擊scrollView上的button, 切換對應的childViewController    func menuButtonClick(button: UIButton){        let toVC = buttonVCMapping[button]        if toVC != nil && toVC != currentVC{                        // 切換介面顯示的childViewController            self.transitionFromViewController(currentVC, toViewController: toVC!, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: nil, completion: { (finished: Bool) -> Void in                if finished{                    toVC!.didMoveToParentViewController(self)                    self.currentVC.willMoveToParentViewController(nil)                                        self.currentVC = toVC                }            })        }    }}

  

運行效果如下:

初始,預設使用button0的childViewController

button0

button1

 

button2

 

button3

 

button4

iOS ChildViewController使用樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.