Use of Tag controllers in iOS development--uitabbarcontroller

Source: Internet
Author: User

Body

Use of Tag controllers in iOS development--uitabbarcontroller

First, 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 array of Viewcontroller

@property (Nullable, nonatomic,copy) nsarray<__kindof uiviewcontroller *> *viewcontrollers;

-(void) Setviewcontrollers: (nsarray<__kindof uiviewcontroller *> * __nullable) viewcontrollers animated: (BOOL) Animated;

The selected Viewcontrolle

@property (Nullable, nonatomic, assign) __kindof Uiviewcontroller *selectedviewcontroller;

Set by numbering to select Viewcontroller

@property (nonatomic) Nsuinteger SelectedIndex;

The navigation controller for the "More" tab when Viewcontroller is greater than 4

@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, and the default is all

@property (nullable, nonatomic, copy) nsarray<__kindof Uiviewcontroller *> *customizableviewcontrollers;

tab bar in label controller

@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;

Set 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 the label layout

-(BOOL) endcustomizinganimated: (bool) animated;

Are you customizing the label layout

-(BOOL) iscustomizing;

Set Tabbar color correlation:

Setting rendering colors affects the rendering of selected fonts and patterns

@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;

When a label is selected, the selected hint pattern behind the label appears after the set item pattern.

@property (Nullable, Nonatomic,strong) UIImage *selectionindicatorimage;

Set the background pattern of a shadow

@property (Nullable, Nonatomic,strong) UIImage *shadowimage

Macro properties of tags in tabbar:

Set the position mode of the label item

@property (nonatomic) uitabbaritempositioning itempositioning;

enumerated as follows

typedef ns_enum (Nsinteger, uitabbaritempositioning) {

uitabbaritempositioningautomatic,//Automatic

uitabbaritempositioningfill,//full

uitabbaritempositioningcentered,//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 divided into black and white two kinds

@property (nonatomic) Uibarstyle Barstyle;

Whether the transparency effect

@property (nonatomic,getter=istranslucent) BOOL translucent;

2, Uitabbardelegate

Called when a label is selected

-(void) TabBar: (Uitabbar *) TabBar Didselectitem: (Uitabbaritem *) item;

When you're about to start editing labels

-(void) TabBar: (Uitabbar *) TabBar willbegincustomizingitems: (Nsarray<uitabbaritem *> *) items; When you have started editing labels

-(void) TabBar: (Uitabbar *) TabBar didbegincustomizingitems: (Nsarray<uitabbaritem *> *) items;

When you are about to enter edit state

-(void) TabBar: (Uitabbar *) TabBar willendcustomizingitems: (Nsarray<uitabbaritem *> *) items changed: (BOOL) Changed

When you have 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 a system type of

-(Instancetype) Initwithtabbarsystemitem: (uitabbarsystemitem) systemitem tag: (nsinteger) tag;

The enumeration of Uitabbarsystemitem is as follows:

typedef ns_enum (Nsinteger, Uitabbarsystemitem) {

uitabbarsystemitemmore,//more icons

uitabbarsystemitemfavorites,//Favorite Icons

uitabbarsystemitemfeatured,//feature icon

uitabbarsystemitemtoprated,//Advanced Icons

uitabbarsystemitemrecents,//Recent Icons

uitabbarsystemitemcontacts,//Contact icon

uitabbarsystemitemhistory,//history icon

uitabbarsystemitembookmarks,//book icon

uitabbarsystemitemsearch,//Find icon

uitabbarsystemitemdownloads,//Download icon

uitabbarsystemitemmostrecent,//record icon

uitabbarsystemitemmostviewed,//All View 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 offset of the caption

@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;

Pattern

@property (Nullable, Nonatomic,strong) UIImage *image;

The pattern at the horizontal screen

@property (Nullable, Nonatomic,strong) UIImage *landscapeimagephone;

Pattern Position Offset

@property (nonatomic) uiedgeinsets imageinsets;

Pattern position offset at horizontal screen

@property (nonatomic) uiedgeinsets landscapeimagephoneinsets;

Set and get font properties for a 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.