IOS UITabBarController and uitabbarcontroller

Source: Internet
Author: User

IOS UITabBarController and uitabbarcontroller

During IOS development, UITabBarController and UINavigationController are also widely used. Similar to UITabBarController, UITabBarController can easily manage multiple controllers and easily switch between controllers.

Procedure:

1. initialize UITabBarController

2. Set rootViewController of UIWindow to UITabBarController.

3. Create a corresponding sub-controller (viewcontroller)

4. Add the sub-controller to UITabBarController.

The Code is as follows:

// Initialize the View Controller UIViewController * vc1 = [[UIViewController alloc] init]; vc1.view. backgroundColor = [UIColor redColor]; UIViewController * vc2 = [[UIViewController alloc] init]; vc2.view. backgroundColor = [UIColor greenColor]; UIViewController * vc3 = [[UIViewController alloc] init]; vc3.view. backgroundColor = [UIColor yellowColor]; UIViewController * vc4 = [[UIViewController alloc] init]; vc4.view. backgroundColor = [UIColor orangeColor]; UIViewController * vc5 = [[UIViewController alloc] init]; vc5.view. backgroundColor = [UIColor purpleColor]; // Add the Controller UITabBarController * tabVC = [[UITabBarController alloc] init] For tabbarController. delegate = self; tabVC. viewControllers = @ [vc1, vc2, vc3, vc4, vc5]; // initialization system UITabBarItem * item1 = [[UITabBarItem alloc] initWithTabBarSystemItem: custom tag: 101]; vc1.tabBarItem = item1; UITabBarItem * item2 = [[UITabBarItem alloc] parts: custom tag: 102]; vc2.tabBarItem = item2; // initialize UITabBarItem UIImage * selImage = [UIImage imageNamed: @ "tabbar_cate_f"]; selImage = [selImage imageWithRenderingMode: Custom]; UITabBarItem * item3 = [[UITabBarItem alloc] initWithTitle: @ "Recent" image: [UIImage imageNamed: @ "tabbar_cate"] selectedImage: selImage]; NSDictionary * dic =@{ region: [UIFont systemFontOfSize: 20], Region: [UIColor redColor]}; [item3 setTitleTextAttributes: dic forState: UIControlStateSelected]; vc3.tabBarItem = item3; // initialize UITabBarItem * item4 = [[UITabBarItem alloc] initWithTitle: @ "" image: [UIImage imageNamed: @ "tabbar_fov"] tag: 104]; vc4.tabBarItem = item4; UITabBarItem * item5 = [[UITabBarItem alloc] tags: UITabBarSystemItemSearch tag: 105]; vc5.tabBarItem = item5; // set TabBarController as the window root controller self. window. rootViewController = tabVC;

2. UITabBar has some ways to change its status, but the tabBar provided by UITabBarController still cannot meet the requirements. Therefore, you need to use custom

The idea is as follows: we need to customize a tabbar. Here we use UIView to replace it. At the same time, we need to add an Item on the tabbar to control the View Controller by clicking. Item involves clicking events, so we can consider using the Button to complete the process, and adding the icon and title on the button.

1. Our custom class JRTabBarController. h

2. implement three methods respectively.

The Code is as follows:

# Pragma mark-loadVC-(void) _ loadVC {self. tabBar. hidden = YES; // 1 Create A View Controller JRViewController * vc1 = [[JRViewController alloc] init]; UIViewController * vc2 = [[UIViewController alloc] init]; vc2.view. backgroundColor = [UIColor greenColor]; UIViewController * vc3 = [[UIViewController alloc] init]; vc3.view. backgroundColor = [UIColor yellowColor]; UIViewController * vc4 = [[UIViewController alloc] init]; vc4.view. backgroundColor = [UIColor blueColor]; self. viewControllers = @ [vc1, vc2, vc3, vc4] ;}# pragma mark-_ makeTabBar-(void) _ makeTabBar {// 1> Custom tabbar UIView * bgview = [[UIView alloc] initWithFrame: CGRectMake (0, kHeight-49, kWidth, 49)]; bgview. backgroundColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.5]; [self. view addSubview: bgview]; // 2> Custom btn CGFloat space = (kWidth-2 * kLeftSpace-5 * kBtSize)/4.0 + kBtSize; for (int I = 0; I <5; I ++) {NSDictionary * dic = _ array [I]; // 1 initialize the button size JRButton * button = [[JRButton alloc] initWithFrame: CGRectMake (kLeftSpace + I * space, 49/2. 0-kBtSize/2.0, kBtSize, kBtSize)]; // 2 initialize the button title and image [button initButtonWithTitle: dic [@ "title"] andImage: dic [@ "image"]; // 3 sets the tag and proxy button for the button. tag = I; button. delegate = self; [bgview addSubview: button]; // 4 Add the button to the array to adjust the position of the selected background [_ btArray addObject: button];} // 3> Add the selected icon _ selectView = [[UIImageView alloc] initWithFrame: CGRectMake (10, 49/2. 0-(kBtSize + 2)/2.0, kBtSize + 4, kBtSize + 4)]; UIButton * button = _ btArray [0]; _ selectView. center = button. center; _ selectView. image = [UIImage imageNamed: @ "bg"]; [bgview addSubview: _ selectView]; [bgview sendSubviewToBack: _ selectView];} # pragma mark-load data-(void) _ loadData {_ btArray = [NSMutableArray array]; NSDictionary * dic1 =@ {@ "title": @ "movie", @ "image": [UIImage imageNamed: @ "movie_cinema"]}; NSDictionary * dic2 =@ {@ "title": @ "news", @ "image": [UIImage imageNamed: @ "msg_new"]}; NSDictionary * dic3 =@ {@ "title": @ "top", @ "image": [UIImage imageNamed: @ "star_top250"]}; NSDictionary * dic4 =@ {@ "title": @ "Cinema", @ "image": [UIImage imageNamed: @ "icon_cinema"]}; NSDictionary * dic5 =@ {@ "title": @ "more", @ "image": [UIImage imageNamed: @ "more_select_setting"]}; _ array = @ [dic1, dic2, dic3, dic4, dic5];}

2. Another function to be completed is the click event. When each Item is clicked, we need to switch the View Controller, while switching the View Controller, we also need to control the movement of small black background images. Here we will implement this method.

# Pragma mark-ButtonDelegate-(void) changePage :( NSInteger) index {// 1 change the selected image UIButton * button = _ btArray [index]; [UIView beginAnimations: nil context: nil]; _ selectView. center = button. center; [UIView commitAnimations]; // 2 switch the View Controller self. selectedIndex = index ;}

3. Define items. Here we define a button to use a custom Item. We add images and titles to the button to achieve our effect. At the same time, we use a proxy to control controls, the Code is as follows:

/*** Initialize the image and title ** @ param title * @ param image */-(void) initButtonWithTitle :( NSString *) title andImage :( UIImage *) image {if (self) {// 1> Add Image UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (self. frame. size. width/2.0-kImageSize/2.0, 2, kImageSize, kImageSize)]; imageView. contentMode = UIViewContentModeScaleAspectFit; imageView. image = image; [self addSubview: imageView]; // 2> Add title UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, kImageSize, self. frame. size. width, self. frame. size. height-kImageSize)]; label. text = title; label. textColor = [UIColor whiteColor]; label. textAlignment = NSTextAlignmentCenter; label. font = [UIFont boldSystemFontOfSize: 13]; [self addSubview: label]; [self addTarget: self action: @ selector (showClick) forControlEvents: UIControlEventTouchUpInside];}

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.