swift-Mapping Add Controller

Source: Internet
Author: User

When using Tabbarcontroller in swift, use the map to add.

Bundle extensions

Import Foundation
extension Bundle {
    //computed Type property is similar to function, no parameter, return value
    var namespace:string {
        return Infodictionary? ["Cfbundlename"] as? String?? ""
    }
}

Tabbarcontroller

Import UIKit class Twtabbarcontroller:uitabbarcontroller {override Func Viewdidload () {super.viewdidload ( )//Add sub-controller Setupchildcontrollers ()}} extension Twtabbarcontroller {private Func Setupchildcon Trollers () {//Use map processing let array = [["Clsname": "Homeviewcontroller", "title": "Home", "imgname": "Home"], ["Clsname": "Towviewcontroller", "title": "Message", "Imgname": "Message_center"], ["Clsname": "Th Reeviewcontroller "," title ":" Discovery "," imgname ":" Discover "], [" Clsname ":" Fourviewcontroller "," title ":" I "," Imgnam E ":" Profile "]," var arraym = [Uiviewcontroller] () for dict in array{Arraym.appe ND (Controller (dict:dict))} viewcontrollers = Arraym} private func Controller (DICT:[STRING:STR ing]), Uiviewcontroller {guard Let Clsname = dict["Clsname"], let title = dict["title"], let imgname = dict[" Imgname "], let CLS = NSCLassfromstring (Bundle.main.namespace + "." + Clsname) as? Uiviewcontroller.type else {return Uiviewcontroller ()}//2 CREATE VIEW Controller//1> will Clsna Me converted to CLS let VC = Cls.init () Vc.title = title//3. Set Image vc.tabBarItem.image = UIImage (named: "Tabbar_" + imgname) Vc.tabBarItem.selectedImage = UIImage (named : "Tabbar_" + imgname + "_selected")? Withrenderingmode (. alwaysoriginal)//Set Tabbar title Color//4. Sets the caption font (size) of the Tabbar vc.tabBarItem.setTitleTextAttributes ([NSAttributedStringKey.foregroundColor:UICo Lor.orange], for:. highlighted)//system default is 12th, modify the font size, to set the Normal font size Vc.tabBarItem.setTitleT Extattributes ([NSAttributedStringKey.font:UIFont.systemFont (Ofsize:12)], For:uicontrolstate (RA wvalue:0))//5 When the navigation controller is instantiated, the push method is called to ROOTVC stack let nav = Twnavigationcontroller (rootviewcontroller: VC) return nAV}}
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.