Two ways to "iOS Dev-31" Uitabbar background, icon color, selected background settings, and hidden Uitabbar

Source: Internet
Author: User

I. Some settings for the Uitabbar background and icon icons


(1) because the background color directly to the Uitabbar display of the impure, translucent feeling, so, sometimes we can directly use a solid color picture as the background to achieve the desired effect;


(2) To change the color of the icon image is also one of the important practical methods, the default is blue.


In the APPDELEGATE.M file: (One navigation controller and 5 view controllers)

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//    Create 5 view controllers and one navigation controller ViewController1 *vc1=[[viewcontroller1 Alloc]init];    Uinavigationcontroller *nav1=[[uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:VC1];    ViewController2 *vc2=[[viewcontroller2 Alloc]init];    ViewController3 *vc3=[[viewcontroller3 Alloc]init];    ViewController4 *vc4=[[viewcontroller4 Alloc]init];    ViewController5 *vc5=[[viewcontroller5 Alloc]init];    Viewcontroller *vc6=[[viewcontroller Alloc]init];    6 titles [email protected] "interface 1";    [email protected] "Interface 2";    [email protected] "Interface 3";    [email protected] "Interface 4";    [email protected] "interface 5";    [email protected] "interface 6";    6 System icon Icons [Nav1.tabbaritem initwithtabbarsystemitem:uitabbarsystemitemtoprated tag:1];    [Vc2.tabbaritem Initwithtabbarsystemitem:uitabbarsystemitemsearch Tag:2]; [Vc3.tabbaritem Initwithtabbarsystemitem:uitabbarsystemitemcontaCTS Tag:3];    [Vc4.tabbaritem initwithtabbarsystemitem:uitabbarsystemitemmostviewed Tag:4];    [Vc5.tabbaritem initwithtabbarsystemitem:uitabbarsystemitemmostrecent Tag:5];    [Vc6.tabbaritem initwithtabbarsystemitem:uitabbarsystemitemdownloads Tag:6]; Create a view controller array and assign it to the Viewcontrollers value of the tab bar Controller Nsarray *arr1=[[nsarray ALLOC]INITWITHOBJECTS:NAV1,VC2,VC3,VC4,VC5,    VC6, nil];    Uitabbarcontroller *tbcon1=[[uitabbarcontroller Alloc]init];        TBCON1.VIEWCONTROLLERS=ARR1; The tab bar Controller has a Tabbar property, this property has two items and the SelectedItem property is not available, because these two properties are directly managed by the tag bar controller, other people can not assign the value//run the following two lines of code, the program will crash//    Tbcon1.tabbar.items=[[nsarray Alloc]initwithobjects:vc1.tabbaritem, nil];        Tbcon1.tabbar.selecteditem=vc1.tabbaritem;        The backgroundcolor can set the label bar color, but is a layer of faint red tbcon1.tabbar.backgroundcolor=[uicolor redcolor]; You can set the background image by setting the background color of the tab bar, such as the red background image, requires the image size to be right//in the following way to obtain the height of the tab bar, create a background image, and then introduce NSLog (@ "%i,%i", (int)    TbCon1.tabBar.frame.size.height, (int) tbCon1.tabBar.frame.size.width); TbcOn1.tabbar.backgroundimage=[uiimage imagenamed:@ "Tabbarbg.png"];        The icon can be set by Tintcolor color Tbcon1.tabbar.tintcolor=[uicolor Redcolor];        Set the background picture of the selected label, the width is 375/5=77 tbcon1.tabbar.selectionindicatorimage=[uiimage imagenamed:@ "Selectiondic.png"];        This tab bar controller as the window's root view controller to display self.window.rootviewcontroller=tbcon1;    Override point for customization after application launch. return YES;}

second, the first way to hide Uitabbar


This approach requires an experiment with the navigation controller view, because we need to use the Hidesbottombarwhenpushed property, which means that when the view is pressed into the stack (the stack of the navigation controller), the bar at the bottom is hidden, including the Uitabbar.


So we experiment with the above nav1, NAV1 's root view controller is VC1, we add a button in the VC1, a click into the VIEWCONTROLLER7.M (instance is VC7), and hide Uitabbar.

In the VC1:

#import "ViewController1.h" #import "ViewController7.h" @interface ViewController1 () @end @implementation viewcontroller1-(void) viewdidload {    //    UIButton *btn1=[uibutton buttonwithtype:uibuttontyperoundedrect];    Btn1.frame=cgrectmake (max., +);    Btn1.backgroundcolor=[uicolor Whitecolor];    [Btn1 settitle:@ "PUSH" forstate:uicontrolstatenormal];    [Btn1 addtarget:self Action: @selector (Jumpto) forcontrolevents:uicontroleventtouchupinside];    [Self.view ADDSUBVIEW:BTN1];            [Super Viewdidload];    Do any additional setup after loading the view.} -(void) jumpto{    ViewController7 *vc7=[[viewcontroller7 alloc]init];    [Self.navigationcontroller pushviewcontroller:vc7 animated:no];} @end

In the VIEWCONTROLLER7.M:

Add a Initwithnibname method #import "ViewController7.h" @interface ViewController7 () @end @implementation viewcontroller7// , the standard is return self. It is also necessary to set its Hidesbottombarwhenpushed property at initialization to take effect//i.e., before the instance of the view controller is loaded into the stack, this property needs to be set, otherwise invalid-(ID) Initwithnibname: ( NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{    self.hidesbottombarwhenpushed=yes;    return self;} @end

third, the second way to hide Uitabbar


is to move the Uitabbar position, that is, adjust the value of the FRAME.ORIGIN.Y. Disappear is to move it out of the screen, appear is to put it back in place. (However, the restoration of the phenomenon, the experience is poor, not recommended)

So, in the VIEWCONTROLLER7.M:

Add a method to try to appear, and in this setting move Tabbar down, remove the entire screen, equivalent to disappear-(void) Viewdidappear: (BOOL) animated{    nsarray *arr1= Self.tabBarController.view.subviews;    UIView *view1=[arr1 objectatindex:0];    Uitabbar *tabbarview1=[arr1 objectatindex:1];    The first view is full screen, does not need to be full height, so can not do any settings    //view1.frame=cgrectmake (0, 0, 375, 667);    Tabbarview1.frame=cgrectmake (0, 667, 375, 49);}

In the VIEWCONTROLLER1.M:

Add a viewdidappear, move the lower tabbar up again, equivalent to restore to situ-(void) Viewdidappear: (BOOL) animated{    nsarray *arr2= Self.tabBarController.view.subviews;    Uitabbar *tabbarview2=[arr2 objectatindex:1];    Tabbarview2.frame=cgrectmake (0, 618, 375, 49);}

Cut a Picture:


Two ways to "iOS Dev-31" Uitabbar background, icon color, selected background settings, and hidden Uitabbar

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.