1. Construction of the project
1.1 Creating a file
1.1.1 Delete template files--Viewcontroller.swift, Main.storyboard
1.1.2 Create a project structure, home directory classess---Level two directory module (function module), model (business model), Tools---> Module subdirectory main,home, Message , Discover,profile
1.1.3 Create the project file, create the corresponding storyboard+ controller for each function module (each view controller inherits from Tabviewcontroller)
* Each storyboard, there is Navigationcontroller + tabviewcontroller composition
* Navigationcontroller is the initial controller, Tabviewcontroller respectively inherits from the corresponding view controller
* TABLE's reusable identifiers are directory names +cell
* Set up the completed file directory
1.2 Adding a child controller
1.2.1 Because it is multi-controller management, the use of code to add a sub-controller
1.2.2 Drag into footage to change the picture's fill mode to the original picture size
Implementation of 1.2.3 Code
Add Child controller private func addchildviewcontrollers () { tabbar.tintcolor = Uicolor.orangecolor () Addchildviewcontroller ("Home", "homepage", "Tabbar_home") Addchildviewcontroller ("message", "Messages", "Tabbar_message_ Center ") Addchildviewcontroller (" Discover "," Discovery "," Tabbar_discover ") Addchildviewcontroller (" Profile "," I "," Tabbar_profile ")}///Add child controller///@param sbname Storyboard name//@param title title/// @param imageName Picture Name private Func Addchildviewcontroller (sbname:string, _ title:string, _ imagename:string) {let sb = Uistoryboard ( Name:sbname, Bundle:nil) let nav = Sb.instantiateinitialviewcontroller () as! Uinavigationcontroller nav.title = title nav.topViewController.title = Title Nav.tabBarItem.image = UIImage (named:imagename) nav.tabBarItem.selectedImage = UIImage (named:imagename + "_highlighted") Addchildviewcontroller (NAV)}
1.3 Custom Tabbar
1.3.1 in 4 controller toggle buttons, add Compose button, customize Tabbar
1.3.2 Create a new Maintabbar.swift file, modify the Tabbar class in storyboard
Lazy loading of the 1.3.3 button
///Compose buttonLazy var Composebutton:uibutton ={Let button=UIButton () button.setimage (UIImage (named:"Tabbar_compose_icon_add"), Forstate:. Normal) Button.setimage (UIImage (named:"tabbar_compose_icon_add_highlighted"), Forstate:. Highlighted) Button.setbackgroundimage (UIImage (named:"Tabbar_compose_button"), Forstate:. Normal) Button.setbackgroundimage (UIImage (named:"tabbar_compose_button_highlighted"), Forstate:. Highlighted) Self.addsubview (button)returnButton} ()
1.3.4 Adjust button position
Override Func Layoutsubviews () { super.layoutsubviews () var index = 0 Let w = Self.bounds.size.width/cgfloa T (buttoncount) let h = self.bounds.size.height for view in Self.subviews as! [UIView] { //Determine child view type if (view is Uicontrol &&!) View is UIButton)) { view.frame = CGRectMake (cgfloat (Index) * W, 0, W, h)
If index + = index = = 1? 2:1 } } self.composeButton.frame = CGRectMake (0, 0, W, h) self.composeButton.center = Cgpointmake ( Self.center.x, H * 0.5)}
1.3.5 adding an event to a button
// Set Compose button click function " Composebuttonclick ", forControlEvents:. Touchupinside)
1.3.6 use Private to modify the function private, click events can not be decorated, because when the System Monitor the Click event, the controller will send a click button message.
Private func Addchildviewcontroller (sbname:string, _ title:string, _ Imagename:string)
1.4 Summary
* Swift syntax is more concise, string concatenation can be directly with the "+" number, but the use of "_" to ignore the parameters do not need to consider, type check more stringent.
* The wording of Swift lazy loading
* Private methods can be modified with private
Swift Project 01