swift TabBarController UINavigationController 组合使用 带有ScrollView和菜单内容以及tableView

来源:互联网 发布:网络平台使用协议书 编辑:程序博客网 时间:2024/05/05 04:38

整个APP都长这个样子.因为没图片.所以做的很丑了点.

接着就为代码区

首先的为登陆界面的代码内容


import UIKitclass LoginViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()                let button = UIButton(type: UIButtonType.RoundedRect)        button.frame = CGRectMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2, 100, 100)        button.setTitle("登陆", forState: UIControlState.Normal)        button.addTarget(self, action: #selector(login), forControlEvents: UIControlEvents.TouchUpInside)        self.view.addSubview(button)    }        func login() {        let tabbarViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("TabBarViewController")        self.presentViewController(tabbarViewController, animated: true, completion: nil)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

整个代码没啥好说的.就定义了一个button,然后点击就从storyBoard里读取定义为TabBarViewController的controller


下面就为TabBarViewController的代码内容

import UIKitclass TabBarViewController: UITabBarController {    var homeViewController: HomeViewController?    var secondViewController: SecondViewController?    var thirdViewController: ThirdViewController?    var fourViewController: FourViewController?        var homeNavigationController: UINavigationController?    var secondNavigationController: UINavigationController?    var thirdNavigationController: UINavigationController?    var fourNavigationController: UINavigationController?            //storyBoard名字    let storyBoard = "Main"        override func viewDidLoad() {        super.viewDidLoad()        setController()            }        // MARK: -- 设置controller    ///设置controller    func setController() {        homeViewController = UIStoryboard(name: storyBoard, bundle: nil).instantiateViewControllerWithIdentifier("HomeViewController") as? HomeViewController        homeViewController?.title = "首页"                        secondViewController = UIStoryboard(name: storyBoard, bundle: nil).instantiateViewControllerWithIdentifier("SecondViewController") as? SecondViewController        secondViewController?.title = "第二页"                thirdViewController = UIStoryboard(name: storyBoard, bundle: nil).instantiateViewControllerWithIdentifier("ThirdViewController") as? ThirdViewController        thirdViewController?.title = "第三页"                fourViewController = UIStoryboard(name: storyBoard, bundle: nil).instantiateViewControllerWithIdentifier("FourViewController") as? FourViewController        fourViewController?.title = "第四页"                homeNavigationController = UINavigationController(rootViewController: homeViewController!)        homeNavigationController?.tabBarItem = UITabBarItem(title: "首页", image: nil, tag: 1)                        secondNavigationController = UINavigationController(rootViewController: secondViewController!)        secondNavigationController?.tabBarItem = UITabBarItem(title: "第二页", image: nil, tag: 2)                thirdNavigationController = UINavigationController(rootViewController: thirdViewController!)        thirdNavigationController?.tabBarItem = UITabBarItem(title: "第三页", image: nil, tag: 3)                fourNavigationController = UINavigationController(rootViewController: fourViewController!)        fourNavigationController?.tabBarItem = UITabBarItem(title: "第四页", image: nil, tag: 4)                self.setViewControllers([homeNavigationController!,secondNavigationController!,thirdNavigationController!,fourNavigationController!], animated: true)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()            }}

上面的代码内容除了前四个viewController是从storyBoard里拿取的,另外的四个UINavigationViewController为定义后添加对应的rootViewController,并设置其对应的tabbarItem内容,图片就暂时没设置.设置了viewController的title内容

在将四个UINavigationViewController添加到TabBarViewController下添加的办法有几个,我使用的是setViewCOntrollers还有就是addChildViewController不同的地方是set是直接放一个数组,而add是得一个一个的增加.最后的设置为是否使用动画效果

接下来为首页内容

import UIKitclass HomeViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {        let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width        let dataArray = ["1","2","3"]        //主界面的tableView    var tableView: UITableView?            override func viewDidLoad() {        super.viewDidLoad()        self.view.backgroundColor = UIColor.redColor()        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "菜单", style: .Plain, target: self, action: #selector(leftBarButtonClick))                        tableView = UITableView(frame: self.view.frame, style: .Grouped)        tableView?.delegate = self        tableView?.dataSource = self        self.view.addSubview(tableView!)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }            // MARK: -左侧菜单    //左侧菜单的点击    func leftBarButtonClick() {            }        //MARK: - tableDelegate    func numberOfSectionsInTableView(tableView: UITableView) -> Int {        return 3    }        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {        if indexPath.section == 0 {            return 180        }else if(indexPath.section == 1) {            return 160        }else {            return 100        }    }        func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {        let headerView = UIView(frame: CGRectMake(0,0,SCREEN_WIDTH , 10))        headerView.backgroundColor = UIColor(red: 239, green: 239, blue: 244, alpha: 1)        return headerView    }        func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {        let footView = UIView(frame: CGRectMake(0,0,SCREEN_WIDTH , 10))        footView.backgroundColor = UIColor(red: 239, green: 239, blue: 244, alpha: 1)        return footView    }        func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {        if section == 0 {            return 1        }else {            return 5        }    }        func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {        return 5    }        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        if section == 2 {            return dataArray.count        }        return 1    }        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        //轮播图        if indexPath.section == 0 {            let cellDentifier = "scrollimage"            var cell = tableView.dequeueReusableCellWithIdentifier(cellDentifier) as? ScrollImage            if cell == nil {                cell = ScrollImage(style: .Default, reuseIdentifier: cellDentifier)            }            cell?.selectionStyle = .None            return cell!                    }else if indexPath.section == 1{ //菜单            let cellDentifire = "Menu"            var cell = tableView.dequeueReusableCellWithIdentifier(cellDentifire) as? Menu            if cell == nil {                cell = Menu(style: .Default, reuseIdentifier: cellDentifire)            }            cell?.selectionStyle = .None            return cell!                    }else {            let cellDentifier = "normal"            var cell = tableView.dequeueReusableCellWithIdentifier(cellDentifier)            if cell == nil {                cell = UITableViewCell(style: .Default, reuseIdentifier: cellDentifier)                cell?.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20)                cell?.textLabel?.text = dataArray[indexPath.row]                print(dataArray[indexPath.row])            }            cell?.selectionStyle = .None            return cell!        }    }}
整个首页的内容为一个tableview.tableview的section的值为3 判断如果为0则就是轮播图,section为1的话则为菜单列表,现在定义的其余情况就为显示数组内的内容.

还有根据section来进行对heard和foot的view重写(应该算重写吧.

0 0
原创粉丝点击