IOS UITabBarController, iostabbarcontroller

Source: Internet
Author: User

IOS UITabBarController, iostabbarcontroller

UITabBarController and UINavigationController are almost standard for iOS apps.

The UITabBarController sub-bar (Label bar) controller, like the UINavigationController navigation Controller, is a controller used to manage the View Controller's container type.

1. Basic use of UITabBarController

(1) create several view Controllers

(2) Add the View Controller to TabBarController

(3) set the window and View Controller as the column controller.

(4) set the display style of the partition Controller

UIViewController * vc1 = [[UIViewController alloc] init]; vc1.view. backgroundColor = [UIColor redColor]; vc1.title = @ "VC1"; // View Controller's column button. If it is not displayed and created, the system will automatically create // vc1.tabBarItem Based on the title of the View Controller; UIViewController * vc2 = [[UIViewController alloc] init]; vc2.view. backgroundColor = [UIColor yellowColor]; vc2.title = @ "VC2"; UIViewController * vc3 = [[UIViewController alloc] init]; vc3.view. BackgroundColor = [UIColor blueColor]; vc3.title = @ "VC3"; UIViewController * vc4 = [[UIViewController alloc] init]; vc4.view. backgroundColor = [UIColor greenColor]; vc4.title = @ "VC4"; // create a bar (Label bar) controller. Like a navigation controller, It is a container controller used to manage view controllers. // Like the navigation controller, the bar controller uses viewControllers to manage its subview controllers UITabBarController * tabBarCtrl = [[UITabBarController alloc] init]; tabBarCtrl. view. backgroundColor = [UIColor cyanColor]; // hand over the viewer in the data to the bar controller for management. // The Bar controller Automatically splits the bar button (UITabBarItem) of the View Controller it manages) put it in the column to display tabBarCtrl. viewControllers = @ [vc1, vc2, vc3, vc4]; // you can set the following view controller of the window as the column controller self. window. rootViewController = tabBarCtrl; // you can obtain the bar UITabBar * tabBar = tabBarCtrl of the bar controller. tabBar; // set the style of the sub-column tabBar. barStyle = UIBarStyleBlack; // whether the tabBar is transparent. translucent = NO; // set the foreground color of the partition tabBar. barTintColor = [UIColor brownColor]; // you can specify the color tabBar for each element. tintColor = [UIColor purpleColor]; // you can specify the tabBar for the selected image in the column. selectionIndicatorImage = [UIImage imageNamed: @ "home.png"]; [self. window makeKeyAndVisible];

Effect

2. Combined use of UITabBarController and UINavigationController

If needed: Click to switch to the next page to hideUITabBarController.

 

 

 

(1) create a partition controller:

// Create a sub-View Controller managed by the sub-controller-(void) createViewControllers {OneViewController * oneVC = [[OneViewController alloc] init]; MyNavgationController * navCtrl1 = [[MyNavgationController alloc] initWithRootViewController: oneVC]; // navCtrl1.tabBarItem = [[UITabBarItem alloc] tags: UITabBarSystemItemSearch tag: 0]; TwoViewController * twoVC = [[TwoViewController alloc] init]; UINavigationController * n AvCtrl2 = [UINavigationController alloc] initWithRootViewController: twoVC]; ThreeViewController * threeVC = [[ThreeViewController alloc] init]; FourViewController * fourVC = [[FourViewController alloc] init]; fiveViewController * fiveVC = [[FiveViewController alloc] init]; SixViewController * sixVC = [[SixViewController alloc] init]; // The tabBarController attribute of the View Controller managed by the partition controller, automatically points to the partition controller. // When the number of view controllers managed by the partition controller exceeds five, a more navigation controller is automatically created, the fifth and later view controllers are automatically added to the more navigation controllers. Self. viewControllers = @ [navCtrl1, navCtrl2, threeVC, fourVC, fiveVC, sixVC];}

 

(2) In MyNavgationController, rewrite the redirection method:

-(Void) pushViewController :( UIViewController *) viewController animated :( BOOL) animated {// 1. retrieve the column UITabBar * tabBar = self. tabBarController. tabBar; // shift the width of the left column of the frame to CGRect frame = tabBar. frame; frame. origin. x-= tabBar. frame. size. width; // animation shadow tabBar [UIView animateWithDuration: 0.28 animations: ^ {tabBar. frame = frame;}]; [super pushViewController: viewController animated: animated];}-(UIViewController *) popViewControllerAnimated :( BOOL) animated {// 1. retrieve the column UITabBar * tabBar = self. tabBarController. tabBar; // shift the width of the left column of the frame to CGRect frame = tabBar. frame; frame. origin. x + = tabBar. frame. size. width; // animation shadow tabBar [UIView animateWithDuration: 0.28 animations: ^ {tabBar. frame = frame;}]; return [super popViewControllerAnimated: YES];}

 

 

3. Custom UITabBarController

 

# Import "MyTabBar. h "@ interface MyTabBarController () <MyTabBarDelegate> // Save the previously selected button @ property (nonatomic, retain) UIButton * preSelBtn; @ end @ implementation MyTabBarController-(void) viewDidLoad {[super viewDidLoad]; [self createViewControllers]; [self createTabBar];} // create View Controller data managed by the bar controller-(void) createViewControllers {FirstViewController * firstVC = [[FirstViewController alloc] init]; Parameters * nav1 = [[checking alloc] parameters: firstVC]; SecondViewController * secondVC = [[SecondViewController alloc] init]; thirdViewController * thirdVC = [[ThirdViewController alloc] init]; FourthViewController * fourthVC = [[FourthViewController alloc] init]; self. viewControllers = @ [nav1, secondVC, thirdVC, fourthVC];} // customize tabBar 49-(void) createTabBar {// 0. hide the tabbar [self. tabBar setHidden: YES]; MyTabBar * tabBr = [[MyTabBar alloc] init]; tabBr. frame = self. tabBar. frame; tabBr. delegate = self; [self. view addSubview: tabBr] ;}# pragma mark-MyTabBarDelegate protocol method-(void) tabBar :( MyTabBar *) tabBar didSelectItemWithIndex :( NSUInteger) toIndex {// set the View Controller self selected by the partition controller. selectedIndex = toIndex ;}

 

@class MyTabBar;@protocol MyTabBarDelegate <NSObject>@optional- (void)tabBar:(MyTabBar *)tabBar didSelectItemWithIndex:(NSUInteger)toIndex;- (void)tabBar:(MyTabBar *)tabBar didSelectItemWithIndex:(NSUInteger)toIndex fromIndex:(NSUInteger)fromIndex;@end@interface MyTabBar : UIImageView@property (nonatomic, assign) id <MyTabBarDelegate> delegate;@end
# Import "MyTabBar. h" # define TABBAR_BTN_START_TAG 100 # define BTN_COUNT 4 @ interface TabBarButton: UIButton @ end @ implementation TabBarButton // rewrite the highlighted method. In this method, we ignore the highlighted state of the button-(void) setHighlighted :( BOOL) highlighted {// NSLog (@ "highlighted = % d", highlighted ); // [super setHighlighted: NO] ;}@ end @ interface MyTabBar () // Save the button selected before the column sharding @ property (nonatomic, retain) TabBarButton * preSelBtn; @ end @ implementation MyTabBar-(instancetype) init {if (self = [super init]) {NSLog (@ "init");} return self ;} // The init method calls initWithFrame to add sub-controls to the control and add them to initWithFrame (instancety Pe) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {// 1.2 sets the background image self of the background view. image = [UIImage imageNamed: @ "tabbg.png"]; // 1.3 set the user interaction attribute to be available (if the parent control cannot interact with the user, all child controls cannot interact with the user) self. userInteractionEnabled = YES; // 2. add the button for (int I = 0; I <BTN_COUNT; I ++) {TabBarButton * btn = [TabBarButton buttonWithType: UIButtonTypeCustom] On the background; // set the NSString * imageName = [NSString stri NgWithFormat: @ "tab _ % d", I]; [btn setImage: [UIImage imageNamed: imageName] forState: UIControlStateNormal]; // set the NSString * imageSelName = [NSString stringWithFormat: @ "tab_c % d", I]; [btn setImage: [UIImage imageNamed: imageSelName] forState: UIControlStateSelected]; // The first button is selected by default if (I = 0) {btn. selected = YES; self. preSelBtn = btn;} // Click Event of the listener button [btn addTarget: self action: @ selector (btnCl Ick :) forControlEvents: UIControlEventTouchDown]; btn. tag = TABBAR_BTN_START_TAG + I; // Add the button to the background view to display [self addSubview: btn] ;}} return self ;}- (void) btnClick :( TabBarButton *) btn {// 1. deselect the selected status self. preSelBtn. selected = NO; // 2. set the selected button to btn. selected = YES; // 3. save the selected button self. preSelBtn = btn; // If the proxy implements the Protocol method, it notifies the code that the buttons on the current control are clicked through the Protocol method. If ([self. delegate respondsToSelector: @ selector (tabBar: didSelectItemWithIndex :)]) {[self. delegate tabBar: self didSelectItemWithIndex: btn. tag-TABBAR_BTN_START_TAG] ;}// this method is called after the frame (size) of the control is modified-(void) layoutSubviews {// This line of code cannot be less than [super layoutSubviews]; // retrieve the number of child controls NSUInteger count = self. subviews. count; // adjust the size of the Child control CGFloat btnY = 0; CGFloat btnW = self. bounds. size. width/count; CGFloat btnH = self. bounds. size. height; int I = 0; // retrieve all the child controls and adjust frame for (TabBarButton * btn in self. subviews) {// set frame CGFloat btnX = btnW * I; btn. frame = CGRectMake (btnX, btnY, btnW, btnH); I ++ ;}}

 

Effect

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.