[IOS development-31] UITabBar background, icon color, selected background settings, and hide UITabBar
I. settings of UITabBar background and icon
(1) because the background color set for UITabBar is not pure and translucent, sometimes we can use a solid color image as the background to achieve the desired effect;
(2) changing the color of an icon is also an important practical method. The default value is blue.
In the AppDelegate. m file: (1 navigation controller and 5 view controllers)
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// create five view controllers and one navigation controller ViewController1 * vc1 = [[ViewController1 alloc] init]; UINavigationController * nav1 = [[UINavigationController alloc] region: 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]; // six titles nav1.title = @ "interface 1 "; vc2.title = @ "interface 2"; vc3.title = @ "Interface 3"; vc4.title = @ "interface 4"; vc5.title = @ "interface 5"; vc6.title = @ "interface 6 "; // six system icons [nav1.tabBarItem initWithTabBarSystemItem: Release tag: 1]; [vc2.tabBarItem release: UITabBarSystemItemSearch tag: 2]; [vc3.tabBarItem initwithtabsystembaritem: Release tag: 3]; [vc4.tabBarItem detail: Comment tag: 4]; [vc5.tabBarItem initWithTabBarSystemItem: Comment tag: 5]; [vc6.tabBarItem initWithTabBarSystemItem: Comment tag: 6]; // create a View Controller array, assign the viewControllers value to the Controller of the label bar NSArray * arr1 = [[NSArray alloc] initWithObjects: nav1, vc2, vc3, vc4, vc5, vc6, nil]; UITabBarController * tbCon1 = [[UITabBarController alloc] init]; tbCon1.viewControllers = arr1; // The label bar controller has a tabBar attribute, which has two items and selectedItem attributes, which are unavailable, because these two attributes are directly managed by the label bar controller, other people cannot assign values to them // run the following two lines of code, and the program will crash // tbCon1.tabBar. items = [[NSArray alloc] initWithObjects: vc1.tabBarItem, nil]; // tbCon1.tabBar. selectedItem = vc1.tabBarItem; // you can use backgroundColor to set the color of the label bar, but it is a pale red tbCon1.tabBar. backgroundColor = [UIColor redColor]; // you can set the background color for the tab bar by setting a background image, such as a red background image, the image size must be exactly the same. // after obtaining the label bar width and height, create a background image and introduce it to NSLog (@ "% I, % I", (int) tbCon1.tabBar. frame. size. height, (int) tbCon1.tabBar. frame. size. width); tbCon1.tabBar. backgroundImage = [UIImage imageNamed: @ "tabBarbg.png"]; // you can set the color tbCon1.tabBar for the icon through tintColor. tintColor = [UIColor redColor]; // sets the background image of the selected tag. The width is 375/5 = 77 tbCon1.tabBar. selectionIndicatorImage = [UIImage imageNamed: @ "selectionDic.png"]; // use this label bar controller as the window Root View Controller to display self. window. rootViewController = tbCon1; // Override point for customization after application launch. return YES ;}
2. The first way to hide UITabBar
This method requires experiment using the navigation controller view, because we need to use the hidesBottomBarWhenPushed attribute, which means that when the view is pushed to the stack (the stack of the navigation Controller ), hide the bar at the bottom, including UITabBar.
So we did experiments on nav1 above. The Root View Controller of nav1 is vc1. We added a button in vc1, clicked it to ViewController7.m (the instance is vc7), and hidden UITabBar.
In vc1:
#import "ViewController1.h"#import "ViewController7.h"@interface ViewController1 ()@end@implementation ViewController1- (void)viewDidLoad { // UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn1.frame=CGRectMake(38, 80, 300, 30); 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 ViewController7.m:
# Import "ViewController7.h" @ interface ViewController7 () @ end @ implementation ViewController7 // Add an initWithNibName method, which is standard with return self. In addition, you need to set its hidesBottomBarWhenPushed attribute to YES during initialization to take effect. That is, you need to set this attribute before the View Controller instance is loaded to the stack, otherwise it is invalid-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {self. hidesBottomBarWhenPushed = YES; return self ;}@ end
3. Second Method of hiding UITabBar
It is to move the location of UITabBar, that is, to adjust the value of frame. origin. y. If it disappears, it is removed from the screen. If it appears, it is put back. (However, it is not recommended because the restoration is slow and the user experience is poor)
Therefore, in ViewController7.m:
// Add a method to try to appear soon, and move the tabBar down here to remove the entire screen, which is equivalent to disappearing-(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 without filling up the height, therefore, no settings can be made. // view1.frame = CGRectMake (0, 0,375,667); tabBarView1.frame = CGRectMake (0,667,375, 49 );}
In ViewController1.m:
// Add a viewDidAppear to move the tabBar down and back it to the original location-(void) viewDidAppear :( BOOL) animated {NSArray * arr2 = self. tabBarController. view. subviews; UITabBar * tabBarView2 = [arr2 objectAtIndex: 1]; tabBarView2.frame = CGRectMake (0,618,375, 49 );}
In fact, there is another method of hiding, but it is extremely lethal. It is completely hidden, that is, the tag controller is set from the root to not display UITabBar. The second line of code is as follows:
UITabBarController *tbCon1=[[UITabBarController alloc]init]; tbCon1.tabBar.hidden=YES;
Screenshot:
There are some issues with the UITabBarController developed by IOS.
Specify each uitabbaritem in the uitabbarcontroller assembly code, instead of in each included viewcontroller.
UITabbarController * tabController = [[UITabbarController alloc] init];
Self. vc1 = [[UIViewController alloc] init];
Slef. vc1.tabbarItem = ......;
Self. vc2 = [[UIViewController alloc] init];
Self. vc2.tabbarItem = ....;
......
NSArray * vcs = @ [self. vc1, self. vc2,...];
[TabController setViewControllers: vcs];
Can I send a video tutorial UI for iOS development: 63 UITabBarController's sub-level mov or download link?
IOS development video tutorial UI: 6.3 UITabBarController hierarchy. mov seed:
Thunder: // QUFodHRwOi8vYWlrYW5keS5vcmcvaU9T5byA5Y + authorization + ezuy5tb3Y/authorization
Adopted! Be a moral audience