Swift中Tabbar的簡單練習(xí)

首先創(chuàng)建根tabbarController作為window的根視圖,在tabbarController中進(jìn)行添加子視圖控制器,這樣就方便管理和層次感更好一些

 func addChildViewController() {
    //主頁
    addChileVC(childVC: FindViewController(),title:"發(fā)現(xiàn)",image:"tab0",selectedImage:"tab0on")
    //關(guān)注
    addChileVC(childVC: FollowViewController(), title: "關(guān)注", image: "tab1", selectedImage: "tab1on")
    //我
    addChileVC(childVC: MeViewController(), title: "我的", image: "tab2", selectedImage: "tab2on")
}

這樣對于子控制器的管理就會更加簡單

/**
添加子試圖控制器
:param: childVC NavigationViewController的根視圖類型
 :param title tabbar item的文字
 :param image tabbar item的默認(rèn)圖片的名稱
 :param selectedImage tabbar item 的選中狀態(tài)下圖片的名稱
 */

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)
    //設(shè)置點擊之后的顏色
    childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
    //設(shè)置導(dǎo)航控制器
    let  childNavigation = UINavigationController.init(rootViewController:childVC)
    
    addChildViewController(childNavigation)
    
}

在子視圖控制器Find中進(jìn)行做跳轉(zhuǎn)和傳值操作

 //設(shè)置背景顏色
    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 = "請輸入內(nèi)容?。?!"
    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("點我跳轉(zhuǎn)", 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)
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 代碼創(chuàng)建UIWindow對象 Xcode7之后使用代碼創(chuàng)建UIWindow對象: //創(chuàng)建UIWindow對象 s...
    云之君兮鵬閱讀 1,492評論 0 2
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,715評論 1 14
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,075評論 4 61
  • 本文參加#感悟三下鄉(xiāng),青春筑夢行#活動,本人承諾,文章內(nèi)容為原創(chuàng),且未在其他平臺發(fā)表過。為響應(yīng)校團(tuán)委關(guān)于“大學(xué)生...
    蘇三哥閱讀 278評論 0 0
  • 職場中有這樣四個字”金三銀四“,即每年春節(jié)過后的三四月份就是跳槽和找工作最集中的兩個時期。因此,找工作與招人成為人...
    欲善坊閱讀 360評論 0 2

友情鏈接更多精彩內(nèi)容