Use of Tag controllers in iOS development--uitabbarcontroller

Source: Internet
Author: User

Use of Tag controllers in iOS development--uitabbarcontrollerFirst, Introduction

Similar to the navigation controller, the label controller is also used to manage the view controller of a UI control, within which a tab bar is encapsulated, and navigation is different, the navigation is managed vertically, with push and pop switch controller, the management of the label is horizontal, through the switch of the label to change the controller, Generally we are accustomed to tabbar as the root view controller of the application, where the navigation is added, and the Viewcontroller is managed in the navigation.

Second, create a label controller

With the following steps, we can easily create a tabbarcontroller:

uitabbarcontroller * tabbar= [[uitabbarcontroller alloc]init];     nsmutablearray * controllerarray = [[nsmutablearray alloc]init];     for  (int i=0; i<4; i++)  {         uiviewcontroller * con = [[uiviewcontroller alloc]init];         [con loadViewIfNeeded];         Con.view.backgroundcolor = [uicolor colorwithred:arc4random ()%255/255.0 green:arc4random () %255/255.0 blue:arc4random ()%255/255.0 alpha:1];         con.tabbaritem.image = [uiimage imagenamed:@ "Btn_publish_face_a.png"];         con.tabbaritem.title=[nsstring stringwithformat:@ "%d",i+1];         con.title  = [nsstring stringwithformat:@ "%d", i+1];        [ Controllerarray addobject:con];    }    tabbar.viewcontrollers  = controllerarray;    [self presentviewcontroller:tabbar animated:yes  completion:nil];

You can easily switch the controller by clicking the label button below. If we have more than 4 controllers, we will create a more navigation system, and we can adjust the order of the controllers by the system's own edits, as follows:

The properties and methods of Uitabbarcontroller
Managed Viewcontroller Array @property (nullable, nonatomic,copy)  nsarray<__kindof uiviewcontroller  *> *viewControllers;-  (void) Setviewcontrollers: (Nsarray<__kindof uiviewcontroller  *> * __nullable) viewcontrollers animated: (BOOL) animated;//selected Viewcontrolle@property ( Nullable, nonatomic, assign)  __kindof uiviewcontroller *selectedviewcontroller;// Viewcontroller@property (nonatomic)  nsuinteger selectedindex;//when Viewcontroller is greater than 4, get "more" with the numbering setting selected Label navigation Controller @property (nonatomic, readonly)  uinavigationcontroller *morenavigationcontroller; / /This property is set to a view controller that can be customized in order, as shown in the second image above, by default all @property (nullable, nonatomic, copy)  nsarray<__ kindof uiviewcontroller *> *customizableviewcontrollers;//tab Bar @property (Nonatomic, ReadOnly)  uitabbar *tabbar ns_available_ios (3_0);//Agent @property (Nullable, nonatomic,weak)  id<uitabbarcontrollerdelegate> delegate; 
Iv. about the tab bar Tabbar

By customizing some of the properties of the tab bar, we can use Tabbar more flexibly.

1. Uitabbar Properties and methods

Settings tab:

@property (nullable,nonatomic,copy) Nsarray<uitabbaritem *> *items; Sets the selected label @property (nullable,nonatomic,assign) Uitabbaritem *selecteditem; -(void) Setitems: (Nullable nsarray<uitabbaritem *> *) Items animated: (BOOL) animated;

To set the custom label order:

Calling this method pops up a controller similar to the second one above, and we can swap the layout order of the labels-(void) Begincustomizingitems: (Nsarray<uitabbaritem *> *) items;   Complete label layout-(BOOL) endcustomizinganimated: (bool) animated; Whether you are customizing the label layout-(BOOL) iscustomizing;

Set Tabbar color correlation:

Setting the rendering color affects the rendering of the selected font and pattern @property (null_resettable, Nonatomic,strong) uicolor *tintcolor;//Set the color of the navigation bar @property ( Nullable, Nonatomic,strong) Uicolor *bartintcolor;

To set a background pattern:

Set the navigation bar background pattern @property (nullable, Nonatomic,strong) UIImage *backgroundimage;//setting when a label is selected, the selected hint pattern behind the label This will appear at the back of the set item pattern @property (Nullable, Nonatomic,strong) UIImage *selectionindicatorimage;//Set the background pattern of the shadow @property ( Nullable, Nonatomic,strong) UIImage *shadowimage

Macro properties of tags in tabbar:

Set the label item's position mode @property (nonatomic) Uitabbaritempositioning itempositioning;//enumeration as follows TypeDef ns_enum (Nsinteger, uitabbaritempositioning) {uitabbaritempositioningautomatic,//auto uitabbaritempositioningfill,//full UITabBarItemPos itioningcentered,//Center} ns_enum_available_ios (7_0);//Set Item width @property (nonatomic) cgfloat itemwidth;// Set Item spacing @property (nonatomic) cgfloat itemspacing;

Similar to the navigation bar, you can also set the Tabbar style and transparency effects:

Style split black and white two @property (nonatomic) Uibarstyle barstyle;//whether transparent effect @property (nonatomic,getter=istranslucent) BOOL Translucent;
2, Uitabbardelegate
Called when the label is selected-(void) TabBar: (Uitabbar *) TabBar Didselectitem: (Uitabbaritem *) item;//when the label is about to start editing-(void) TabBar: (Uitabbar *)          TabBar willbegincustomizingitems: (Nsarray<uitabbaritem *> *) items;           When you have started editing labels-(void) TabBar: (Uitabbar *) TabBar didbegincustomizingitems: (Nsarray<uitabbaritem *> *) items; To enter edit state-(void) TabBar: (Uitabbar *) TabBar willendcustomizingitems: (Nsarray<uitabbaritem *> *) Items change D: (BOOL) changed; Has entered edit state-(void) TabBar: (Uitabbar *) TabBar didendcustomizingitems: (Nsarray<uitabbaritem *> *) items changed: ( BOOL) changed;
Five, see Uitabbaritem again

Like Navigationitem, item on the tab bar can also be customized, some methods are as follows.

Initialization method:

Create by Title and pattern-(instancetype) Initwithtitle: (Nullable NSString *) Title Image: (Nullable UIImage *) image tag: (Nsinteger) tag;-(Instancetype) Initwithtitle: (Nullable NSString *) Title Image: (Nullable UIImage *) Image selectedimage: (Nullable UIImage *) selectedimage;//Create System Type-(Instancetype) Initwithtabbarsystemitem: (uitabbarsystemitem) systemitem tag: ( Nsinteger) tag;

The enumeration of Uitabbarsystemitem is as follows:

typedef ns_enum (Nsinteger, Uitabbarsystemitem) {uitabbarsystemitemmore,//More icons uitabbarsystemitemfavorites,//favorites Icon uitabbarsystemitemfeatured,//feature icon uitabbarsystemitemtoprated,//advanced icon uitabbarsystemitemrecents,//recent icon UITabBarSys temitemcontacts,//Contact icon uitabbarsystemitemhistory,//History icon uitabbarsystemitembookmarks,//book icon UITabBarSystemItemSea rch,//find icon uitabbarsystemitemdownloads,//download icon uitabbarsystemitemmostrecent,//record icon uitabbarsystemitemmostviewed,// View all icons};

Uitabbaritem Common Properties:

Set the check pattern @property (nullable, Nonatomic,strong) UIImage *selectedimage;

The following property allows you to set the header text for item:

Con.tabBarItem.badgeValue = @ "1";

Set the position of the caption offset @property (nonatomic, ReadWrite, assign) Uioffset titlepositionadjustment;

Since Uitabbaritem is inherited from Uibaritem, there are also the following properties that can be set using:

Title @property (nullable, nonatomic,copy)               nsstring    *title;   //Patterns      @property ( Nullable, nonatomic,strong)            uiimage      *image;  //pattern        @property on horizontal screen ( Nullable, nonatomic,strong)            uiimage      *landscapeimagephone;//Pattern Position offset @property (nonatomic)                    UIEdgeInsets imageInsets;  //pattern position offset at horizontal @property (nonatomic)                    uiedgeinsets landscapeimagephoneinsets ;//sets and gets the font properties of the caption-  ( void) Settitletextattributes: (Nullable nsdictionary<nsstring *,id> *) Attributes forstate: (UIControlState) state;-  ( nullable nsdictionary<nsstring *,id> *) Titletextattributesforstate: (UIControlState) state;



Use of Tag controllers in iOS development--uitabbarcontroller

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.