美文网首页Swift
Swift中Tabbar的简单练习

Swift中Tabbar的简单练习

作者: Coder东 | 来源:发表于2017-05-08 11:42 被阅读14次

    首先创建根tabbarController作为window的根视图,在tabbarController中进行添加子视图控制器,这样就方便管理和层次感更好一些

     func addChildViewController() {
        //主页
        addChileVC(childVC: FindViewController(),title:"发现",image:"tab0",selectedImage:"tab0on")
        //关注
        addChileVC(childVC: FollowViewController(), title: "关注", image: "tab1", selectedImage: "tab1on")
        //我
        addChileVC(childVC: MeViewController(), title: "我的", image: "tab2", selectedImage: "tab2on")
    }
    

    这样对于子控制器的管理就会更加简单

    /**
    添加子试图控制器
    :param: childVC NavigationViewController的根视图类型
     :param title tabbar item的文字
     :param image tabbar item的默认图片的名称
     :param selectedImage tabbar item 的选中状态下图片的名称
     */
    
    func addChileVC(childVC:UIViewController,title:String,image:String,selectedImage:String)  {
        childVC.title = title;
        childVC.tabBarItem.image = UIImage.init(named:image)
        childVC.tabBarItem.selectedImage = UIImage.init(named:selectedImage)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置点击之后的颜色
        childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
        //设置导航控制器
        let  childNavigation = UINavigationController.init(rootViewController:childVC)
        
        addChildViewController(childNavigation)
        
    }
    

    在子视图控制器Find中进行做跳转和传值操作

     //设置背景颜色
        self.view.backgroundColor = UIColor.cyan
         NotificationCenter.default.addObserver(self, selector: #selector(notification(obj:)), name:NSNotification.Name(rawValue: "NotificationName") , object: nil)
        //初始化
        textField = UITextField.init(frame: CGRect(x:0,y:0,width:200,height:30))
        textField.center = CGPoint.init(x:self.view.center.x, y:200)
      //textField.textColor = UIColor.blue
      // textField.backgroundColor = UIColor.green
        textField.placeholder = "请输入内容!!!"
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.layer.borderColor = UIColor.black.cgColor
        textField.layer.borderWidth = 0.5
        view.addSubview(textField)
        
        let sendBtn = UIButton.init(frame:CGRect(x:0,y:0,width:100,height:30))
        sendBtn.backgroundColor = UIColor.orange
        sendBtn.setTitle("点我跳转", for: UIControlState.normal)
        sendBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        sendBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
        sendBtn.center = CGPoint.init(x: self.view.center.x, y: 280);
        sendBtn.addTarget(self, action: #selector(sendBtnClick), for: UIControlEvents.touchUpInside)
        view.addSubview(sendBtn)
        
    //方法
    func notification(obj: NSNotification)  {
    }
    func sendBtnClick() {
        let vc = DetailViewController()
        vc.hidesBottomBarWhenPushed = true
        vc.text = self.textField.text!
        self.navigationController?.pushViewController(vc, animated: true)
    }
    

    相关文章

      网友评论

        本文标题:Swift中Tabbar的简单练习

        本文链接:https://www.haomeiwen.com/subject/ssijtxtx.html