ios_20_ Micro Bo Skeleton Construction

Source: Internet
Author: User
Tags set background

Eventually:



Beyondviewcontroller.m

beyondviewcontroller.m//20_ Handsome no Weibo////Created by Beyond on 14-8-3.//Copyright (c) 2014 Com.beyond. All rights reserved.//This is the main controller, divided into two pieces, the following is the dock bar, which shows the different sub-controller view, sub-controller preferably with the navigation controller packaging, this way the controller comes with the navigation bar, left button, title, right button/* Cannot click, Or click no response reason: userinteractionenabled = no; Hidden = YES Alpha <= 0.01 clearcolor, view color is transparent, can not be clicked */#import "BeyondViewController.h" #import "Dock.h" #import "do CkBtn.h "#import" Column.h "//Host Controller The height of the dock under the # define Kdockheight 44@interface Beyondviewcontroller () {//Number of Column objects loaded from plist        Group Nsmutablearray *_columns;        Dock Options Bar under the host controller dock *_dock; Records the currently selected sub-controller in order to remove its view from the parent controller's view, preparing the view for adding a new sub-controller Uiviewcontroller *_CURRENTCHILDVC;}    @end @implementation beyondviewcontroller-(BOOL) prefersstatusbarhidden{return NO;    }-(void) viewdidload{[Super Viewdidload];    0. From plist load column array, iterate the array, according to the dictionary, generate a Column object, deposited in the Column object array _columns = [Nsmutablearray array];    NSBundle *mainbundle = [NSBundle mainbundle]; NSString *fullpath = [Mainbundle pathforresource:@ "Columnlist.plist" oftype:nil];        Nsarray *arr = [Nsarray Arraywithcontentsoffile:fullpath];        For (Nsdictionary *dict in arr) {column *column = [column Columnwithdict:dict];    [_columns Addobject:column];        }//1. Add the dock below the main controller side [self adddock];        2. Once all the sub-controllers are created, and the childviewcontrollers [self createallchildviewcontrollers] is added to the current controller after being wrapped in the navigation;        3. The No. 0 controller is selected by default [self changechildviewatindex:0 andchildvcclassname:@ "Homeviewcontroller"];   4. Once set the global navigation bar above the color theme style [self setglobalnavigationitemcolortheme];        } #pragma mark added dock-(void) adddock{//1. Add the dock below the main controller side _dock = [[Dock alloc] init];    2. Listen for btn clicks inside the dock, make the controller a proxy property on the dock, or assign a value __unsafe_unretained beyondviewcontroller *beyond = Self for the member Blok of the dock; _dock.btnclickblock = ^ (dockbtn *btn) {//Call custom method, change child view, Parameter 1: index number, Parameter 2: Class name of sub-controller [Beyond Changechildviewatind    Ex:btn.tag AndChildVCClassName:btn.viewControllerClassName];            }; 3, set FR on dockAme _dock.frame = CGRectMake (0, Self.view.frame.size.height-kdockheight, Self.view.frame.size.width, kdockheight);    Log (@ "_dock frame--%@", Nsstringfromcgrect (_dock.frame));        4, add the dock to the main controller side of view [Self.view Addsubview:_dock]; 2. Iterate through the Column object array and add the dockbtn for (Column *column in _columns) {[_dock AddDockBtnWithIconName:column.col] in the dock in bulk    Umnimgname Title:column.columnName ViewControllerClassName:column.columnClassName];    }//3. Set the dock by default No. 0 [_dock setdockbtnclickedatindex:0]; }//custom method, changing the child view, Parameter 1: index number, Parameter 2: Class name of the sub-controller-(void) Changechildviewatindex: (int) index Andchildvcclassname: (NSString *)    viewcontrollerclassname{log (@ "clicked%@", Viewcontrollerclassname);        if (Self.childViewControllers.count > 0) {//0, first remove the new sub-controller, if the new sub-controller is the current controller, directly return it OK?        Uiviewcontroller *CHILDVC = [self childviewcontrollers][index];        if (CHILDVC = = _CURRENTCHILDVC) return; 1, first remove the previous sub-controller's view [_currentchildvc.view Removefromsuperview];                2. Add a new sub-controller view to the host controller's view childVC.view.frame = CGRectMake (0, 20, 320, 416);        Log (@ "Self view--%@", Nsstringfromcgrect (Self.view.frame));        Log (@ "CHILDVC view--%@", Nsstringfromcgrect (ChildVC.view.frame));                The view is not added repeatedly, because once a view is added repeatedly, it is placed on top, preferably by removing the old view and adding a new view [Self.view AddSubview:childVC.view];    3, important, the current sub-controller must be updated for the next removal to be prepared _CURRENTCHILDVC = CHILDVC; }} #pragma mark creates all the sub-controllers (altogether 5, first side, message, I, square, more)-(void) createallchildviewcontrollers{//1. Iterate through the array of Column objects, create all the sub-controllers in batches, Wrapped with a navigation controller and finally added to the self childviewcontrollers array to save for (Column *column in _columns) {Class c = nsclassfromstring (col        Umn.columnclassname);        Uiviewcontroller *CHILDVC =nil; if ([Nsstringfromclass (c) isequaltostring:@ "Moreviewcontroller"]) {//Special note: After inheriting the TableView, to use the group style again, you must create the        When specifying a style of group, here specifically refers to moreviewcontroller CHILDVC = [[C alloc]initwithstyle:uitableviewstylegrouped]; } else {CHILDVC = [[C Alloc]init];        }//Set the title of the navigation bar childVC.navigationItem.title = Column.columnname;    Override the parent class method [self ADDCHILDVIEWCONTROLLER:CHILDVC]; }} #pragma Marck-Override the method of the parent class//In order to wrap all the navigation controllers when the child controller is added, override the Addchildviewcontroller method-(void) Addchildviewcontroller: ( Uiviewcontroller *) childvc{Uinavigationcontroller *nav = [[Uinavigationcontroller alloc]initwithrootviewcontroller:    CHILDVC]; Add the sub-controllers that are packaged as navigation controllers to the host controller so that each sub-controller has its own unique navigation bar [Super Addchildviewcontroller:nav]; 4. Once set the global navigation bar above the color theme style-(void) setglobalnavigationitemcolortheme{//1. Navigation bar//1.1. Operation NavBar pretty much all the navigation bars in the entire app Uinav        Igationbar *navbar = [Uinavigationbar appearance]; 1.2. Set the navigation bar Uinavigationbar background picture (stretch) [NavBar setbackgroundimage:[uiimage imagestretchedwithname:@ "Navigationbar_    Background.png "] forbarmetrics:uibarmetricsdefault];    1.3. Set the status bar background, no effect???            [UIApplication sharedapplication].statusbarstyle = uistatusbarstylelightcontent; 1.4. Set the title Text property of the navigation bar Uinavigationbar, set nsmutabledictionary *navigationbartitledict = [nsmutabledictionary    Dictionary];    foreground color, that is, the colour of the text [navigationbartitledict Setobject:[uicolor Darkgraycolor] forkey:nsforegroundcolorattributename]; Text shadow cancellation, the dictionary can not put structure, to use nsvalue packaging [navigationbartitledict setobject:[nsvalue Valuewithuioffset:uioffsetzero] ForKey            : Nsshadowattributename];    2. Item Uibarbuttonitem *barbtnitem =[uibarbuttonitem appearance] above the navigation bar; 2.1. Set background//button normal background [Barbtnitem setbackgroundimage:[uiimage imagenamed:@ "Navigationbar_button_background.png"    ] Forstate:uicontrolstatenormal Barmetrics:uibarmetricsdefault]; Background [Barbtnitem setbackgroundimage:[uiimage imagenamed:@ "Navigationbar_button_background_pushed.png"] when the button is highlighted    Forstate:uicontrolstatehighlighted Barmetrics:uibarmetricsdefault]; Background [Barbtnitem setbackgroundimage:[uiimage imagenamed:@ "Navigationbar_button_background_disable.png"] when the button is unchecked forstate:uicontrolstatedIsabled Barmetrics:uibarmetricsdefault];    2.2. Set the Text property of the Barbtnitem nsmutabledictionary *baritemtitledict = [Nsmutabledictionary dictionary];    baritemdict text color [baritemtitledict setvalue:[uicolor darkgraycolor] forkey:nsforegroundcolorattributename];        baritemdict font [baritemtitledict setvalue:[uifont systemfontofsize:13] forkey:nsfontattributename];    2.3. Use a dictionary to set the title text attribute of Barbtnitem [Barbtnitem settitletextattributes:baritemtitledict forstate:uicontrolstatenormal]; [Barbtnitem settitletextattributes:baritemtitledict forstate:uicontrolstatehighlighted];} @end

Dock.h

  dock.h//  20_ Handsome no Weibo////  Created by Beyond on 14-8-3.//  Copyright (c) 2014 Com.beyond. All rights reserved.//  Dock is a bar under the main controller, which is composed of buttons dockbtn #import <UIKit/UIKit.h> @class dockbtn;@ Interface dock:uiview//  Add an item to the Dock (View), the parameter is the icon name, and the caption to be displayed, and the class name of the corresponding sub-controller-(void) Adddockbtnwithiconname: ( NSString *) Iconname title: (NSString *) title Viewcontrollerclassname: (NSString *) viewcontrollerclassname;// When a button in the dock is clicked, the code block is invoked to handle the corresponding Click event @property (copy,nonatomic) void (^btnclickblock) (DOCKBTN *);//Custom method, The code determines which dockbtn is clicked, and the parameter is the index of the button that will be clicked in the Dock Bar-(void) Setdockbtnclickedatindex: (int) index; @end


Dock.m

dock.m//20_ Handsome no Weibo////Created by Beyond on 14-8-3.//Copyright (c) 2014 Com.beyond. All rights reserved.//This is the main controller under that column, Tabbar, also known as the Dock, there are five buttons, the home page, I, message, square, more #import "Dock.h" #import "DockBtn.h" @ Interface Dock () {//the DOCKBTN dockbtn *_currentdockbtn is currently selected;} @end @implementation dock//init method calls initwithframne-(ID) initWithFrame: (cgrect) frame{self = [Super initWithFrame:    Frame]; if (self) {//Intrinsic fixed property, set dock background color (classification method, can be tiled using imagename) Self.backgroundcolor = [Uicolor Colorwithpatternima    genamed:@ "Tabbar_background.png"]; } return self;} Provide an interface to the external, add a dockbtn (button) to the dock (View), the parameter is the icon name, and the title to display, and the corresponding sub-controller class name-(void) Adddockbtnwithiconname: (NSString *) Iconname title: (NSString *) title Viewcontrollerclassname: (NSString *) viewcontrollerclassname{//1. Create a button inside the dock,    and add to the dock inside dockbtn *dockbtn = [dockbtn buttonwithtype:uibuttontypecustom];        [Self addsubview:dockbtn]; 2. Set dockbtn text displayed in normal state [dockbtn Settitle:title Forstate:uicontrolstatenOrmal];        3. Classification method, set the button normal and selected picture, return the picture size [dockbtn setbtnimgfornormalandselectedwithname:iconname];        4. Set dockbtn corresponding to click, the class name of the sub-controller to be instantiated [dockbtn Setviewcontrollerclassname:viewcontrollerclassname]; 5. Tap, as long as the response is pressed, (the event is passed to the dock method, the dock method by calling the property block code block, thereby invoking the code inside the host controller, because: in the host controller is instantiated in the dock, the DOCKBTN is instantiated in the dock) , therefore, the host controller does not know the existence of Dockitem) [Dockbtn addtarget:self Action: @selector (Dockbtnclick:) forControlEvents:        Uicontroleventtouchdown]; 6. Traverse the frame that sets all the buttons in the dock (distribute it evenly) [self setdockbtnframes];}         Iterate over the frame that sets all the buttons in the dock (to distribute evenly)-(void) setdockbtnframes{//1, get all the buttons in the dock int dockbtnnum = Self.subviews.count; 2, according to the current number of dockbtn in the dock, calculate the width of each dockbtn (self is dock,320*44) cgfloat dockbtnwidth = self.frame.size.width/d    Ockbtnnum;        CGFloat dockbtnheight = self.frame.size.height;                for (int i = 0; i < Dockbtnnum; i++) {//1. Remove child controls Dockbtn *btn = Self.subviews[i]; 2. Calculate its x btn.frame = Cgrec According to the indexTmake (i * dockbtnwidth, 0, Dockbtnwidth, dockbtnheight);            3. When initializing, the No. 0 btn (that is, the first page) is selected if (i = = 0) {btn.selected = YES; Most importantly, the selected, set as the current button, with the member variables to remember, when you click on the Dock button, the first set to unchecked, and then clicked on the button selected, and finally, the most important thing is to reset the clicked button to the current button, with the member variables to remember _c        URRENTDOCKBTN = BTN;    }//4. Because when you click on the button in the dock, you know which button is clicked, so give each button a tag, as its index btn.tag = i; }}//The most important thing is that when you click on the button on the dock, the current is unchecked, then the button that is clicked is selected, and most importantly, the button that is clicked is reset to the present button, using the member variable to remember-(void) Dockbtnclick: (        DOCKBTN *) btn{//1. Let the current btn uncheck _currentdockbtn.selected = NO;        2. Let the new btn check btn.selected = YES;        3. Finally, let the new btn become the currently selected BTN _currentdockbtn = btn; 4. Call block, which is the code block passed in the master control, to handle the instantiation of the corresponding sub-controller if (_btnclickblock) {////Pass the parameter dockbtn to the host controller, which remembers its corresponding controller    The class name _btnclickblock (BTN);    }}//The custom method, the code determines which dockbtn is clicked, and the parameter is the index of the button that will be clicked in the Dock Bar-(void) Setdockbtnclickedatindex: (int) index{//1.robust judgment if (Index < 0 | | | index >= self.subviews.count) return;        2. Through the index to the corresponding DOCKBTN Viewwithtag also line dockbtn *btn = Self.subviews[index]; 3. Manually call the following method, equivalent to the user by hand click on the dock inside the corresponding button [self dockbtnclick:btn];} @end


DockBtn.h

  dockbtn.h//  20_ Handsome no Weibo////  Created by Beyond on 14-8-4.//  Copyright (c) 2014 Com.beyond. All rights reserved.//  a dockbtn represents a button on the dock that has a member that is the class name of the corresponding sub-controller, such as the home button, and the value of the member property is called: Homeviewcontroller#import <UIKit/UIKit.h> @interface dockbtn:uibutton//Each dockbtn, use a member to remember the class name of the controller it corresponds to @property (nonatomic,copy) nsstring *viewcontrollerclassname; @end


Dockbtn.m

dockbtn.m//20_ Handsome no Weibo////Created by Beyond on 14-8-4.//Copyright (c) 2014 Com.beyond. All rights reserved.//A dockbtn represents a button on the dock that has a member that is the class name of the corresponding sub-controller, such as the home button, and the value of the member property is called: Homeviewcontroller#import " DockBtn.h "//button content Total width # define Kbtncontentwidth contentrect.size.width//button content Total Height # define Kbtncontentheight The height ratio of the picture in the contentrect.size.height//button # define Kimageheightratio 0.6//The height of the text label in the button # define KLABELHEIGHTRATIO (1 -Kimageheightratio) @implementation dockbtn//Some of the default generic properties must be written inside the constructor-(ID) initWithFrame: (cgrect) frame{self = [super in    Itwithframe:frame];        if (self) {///1. Set the button Text property (in the Office, font size) self.titleLabel.textAlignment = Nstextalignmentcenter;                Self.titleLabel.font = [Uifont systemfontofsize:12];        2. Set the button picture properties (zoom mode, Cancel button when the default click highlight color) Self.imageView.contentMode = Uiviewcontentmodescaleaspectfit;                The Cancel button changes color when the default click is highlighted (image is drawn darker when highlighted or pressed) self.adjustsimagewhenhighlighted = NO; // 3. Classification method, set the background when the button is selected [Self setbgimgforselected:@ "tabbar_slider.png"]; } return self;} #pragma mark overrides the method of the parent class (overriding the behavior of the parent class when highlighting)-(void) sethighlighted: (BOOL) highlighted{//Because here the picture is only used when the button is selected and the default state is So to cancel some of the default color-changing behavior of the highlighted state//do not write anything here, that is, cancel, the button itself is highlighted when the behavior of the execution} #pragma mark returns the border of the button interior Uiimageview (the picture in the button is above, centered)-(CGRect) imag Erectforcontentrect: (CGRect) contentrect{//To center, the quickest way is to let the picture width of the button as wide as the button return CGRectMake (0, 0, Kbtncontentwidth, kbtn Contentheight * kimageheightratio);} #pragma mark returns the border of the Uilabel inside the button (the text in the button is below, centered)-(CGRect) Titlerectforcontentrect: (cgrect) contentrect{//To center, The quickest way is to make the label width of the button as wide as the button//the y of the text is located above the bottom line of the picture 5 units distance, that is, the distance above the picture 5 cgfloat labely = kbtncontentheight * Kimageheightrati    o-5;    The height of the text is the total of all the remaining height of the button cgfloat labelheight = kbtncontentheight-labely; Return CGRectMake (0, LabelY, Kbtncontentwidth, labelheight);} @end


Model Column.h

  column.h//  20_ Handsome no Weibo////  Created by Beyond on 14-8-4.//  Copyright (c) 2014 Com.beyond. All rights reserved.//  1 column model corresponds to a button above the dock, category #import <foundation/foundation.h>//data Model Represents a column @interface column:nsobject//column name @property (nonatomic,copy) nsstring *columnname;//column Picture name @property (nonatomic, copy) nsstring *columnimgname;//column corresponds to the controller's class name @property (nonatomic,copy) NSString *columnclassname;//UI control with weak, String with copy, other objects use strong//to provide a class method, the constructor, to return the object that encapsulates the data (return ID also) + (Column *) columnnamed: (NSString *) ColumnName imgname: ( nsstring*) columnimgname className: (NSString *) columnclassname;//class method, dictionary to object similar to JavaBean disposable fill + (Column *) columnwithdict :(Nsdictionary *) dict;//object method, after setting the object's properties, return Object-(Column *) Initwithdict: (Nsdictionary *) dict; @end


model COLUMN.M

column.m//20_ Handsome no Weibo////Created by Beyond on 14-8-4.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "Column.h" @implementation column//Returns an object instance with column corresponding controller name + (column *) Columnnamed: ( NSString *) ColumnName imgname: (NSString *) columnimgname className: (NSString *) columnclassname{//For compatible subclasses use self Colu    MN *column = [[Self alloc]init];    Column.columnname = ColumnName;    Column.columnimgname = Columnimgname;    Column.columnclassname = Columnclassname; return column;} class method, dictionary to object similar to JavaBean one-time padding + (Column *) Columnwithdict: (nsdictionary *) dict{//Just call the object's Initwithdict method, the reason is to use self for the sub-class into the Line compatible return [[Self alloc]initwithdict:dict];} Object method, after setting the object's properties, returns the Object-(Column *) Initwithdict: (nsdictionary *) dict{//must first call the parent class NSObject's Init method if (self = [super init    ] {//Set the object's own properties [self setvaluesforkeyswithdictionary:dict]; }//Returns the filled object return self; @end

The data source for the five column buttons in the dock columnlist.plist







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.