Custom tabbar label bar and custom tabbar label

Source: Internet
Author: User

Custom tabbar label bar and custom tabbar label

Since I learned iOS, I feel that custom things are easier to operate than those that come with the system, and there is more space for modification and execution.

The following describes how to use tabbar.

First, create an FButton class that inherits UIButton and regulates the buttons on the tabbar.

Then, create a RootViewController that inherits UITabbarController as the Root View Controller.

If you don't talk much about it, go directly to the Code:

In the. m file of FButton:

# Import "FButton. h"

@ Implementation FButton

-(Id) initWithFrame :( CGRect) frame
{
If (self = [super initWithFrame: frame]) {

}
Return self;
}

# Pragma mark set image in btn
-(CGRect) imageRectForContentRect :( CGRect) contentRect
{
CGFloat imageW = contentRect. size. width;
CGFloat imageH = contentRect. size. height * 0.65;

Return CGRectMake (0, 3, imageW, imageH );
}
# Pragma mark set title in btn
-(CGRect) titleRectForContentRect :( CGRect) contentRect
{
CGFloat titleY = contentRect. size. height * 0.65;
CGFloat titleW = contentRect. size. width;
CGFloat titleH = contentRect. size. height-titleY;

Return CGRectMake (0, titleY, titleW, titleH );
}

@ End

In the. h file of RootViewController:

# Import <UIKit/UIKit. h>

@ Interface RootViewController: UITabBarController

// Hide tabbar
-(Void) isHiddenCustomTabbar :( BOOL) isHidden;

@ End

In the. m file:

@ Interface RootViewController ()
{
UIImageView * _ tabbarView; // The Custom tabbar overwrites the built-in
FButton * _ previusbtn; // record the previous button
}

@ End

@ Implementation RootViewController

-(Void) viewDidLoad {
[Super viewDidLoad];

// Hide the built-in tabbar
Self. tabBar. hidden = YES;

[Self creatCustomTabbarView];
}
# Pragma mark-create a custom tabbar
-(Void) creatCustomTabbarView
{
// The height of the tabBar label bar is 49.
_ TabbarView = [[UIImageView alloc] initWithFrame: CGRectMake (0, HEIGHT-49, WIDTH, 49)];
_ TabbarView. image = [UIImage imageNamed: @ "tabbar"];
_ TabbarView. userInteractionEnabled = YES;
[Self. view addSubview: _ tabbarView];

HomeViewController * first = [[HomeViewController alloc] init];
UINavigationController * navi1 = [[UINavigationController alloc] initWithRootViewController: first];
ExpertViewController * second = [[ExpertViewController alloc] init];
UINavigationController * navi2 = [[UINavigationController alloc] initWithRootViewController: second];
HospitalViewController * third = [[HospitalViewController alloc] init];
UINavigationController * navi3 = [[UINavigationController alloc] initWithRootViewController: third];
CommunityViewController * fourth = [[CommunityViewController alloc] init];
UINavigationController * navi4 = [[UINavigationController alloc] initWithRootViewController: fourth];
MineViewController * th = [[MineViewController alloc] init];
UINavigationController * navi5 = [[UINavigationController alloc] initWithRootViewController: fifth];

Self. viewControllers = @ [navi1, navi2, navi3, navi4, navi5];

// The following content is customized according to your own requirements.
NSArray * title_arr = @ [@ "Homepage", @ "expert", @ "hospital", @ "Community", @ "my"];
NSArray * arr = @ [@ "home", @ "doctor", @ "hospunity", @ "community", @ "mine"];

For (int I = 0; I <arr. count; I ++ ){
[Self creatButtonWithNormalName: arr [I] andSelectName: [NSString stringWithFormat: @ "selected-% @", arr [I] andTitle: title_arr [I] andIndex: I];
}
// Set the default selected button
FButton * button = _ tabbarView. subviews [0];
[Self changeViewController: button];
}
# Pragma mark: create buttons and text on the tab bar
-(Void) creatButtonWithNormalName :( NSString *) normal andSelectName :( NSString *) selected andTitle :( NSString *) title andIndex :( int) index {

FButton * customButton = [FButton buttonWithType: UIButtonTypeCustom];
CustomButton. tag = index;

CGFloat buttonW = _ tabbarView. frame. size. width/5;
CGFloat buttonH = _ tabbarView. frame. size. height;

CustomButton. frame = CGRectMake (buttonW * index, 0, buttonW, buttonH );
[CustomButton setImage: [UIImage imageNamed: normal] forState: UIControlStateNormal];
[CustomButton setImage: [UIImage imageNamed: selected] forState: UIControlStateDisabled];
[CustomButton setTitle: title forState: UIControlStateNormal];
// Button click event
[CustomButton addTarget: self action: @ selector (changeViewController :) forControlEvents: UIControlEventTouchDown];
// Set the position, font size, and color of the image and label.
CustomButton. imageView. contentMode = UIViewContentModeCenter;
CustomButton. titleLabel. textAlignment = NSTextAlignmentCenter;
UIColor * color = UTILS_COLORRGB (0,154,154 );
[CustomButton setTitleColor: color forState: UIControlStateNormal];
CustomButton. titleLabel. font = [UIFont systemFontOfSize: 11];

[_ TabbarView addSubview: customButton];

}

# Called when the pragma mark button is clicked
-(Void) changeViewController :( FButton *) sender
{
Self. selectedIndex = sender. tag; // switch the interface of different Controllers
Sender. enabled = NO;
If (_ previusbtn! = Sender ){
_ Previusbtn. enabled = YES;
}
_ Previusbtn = sender;
Self. selectedViewController = self. viewControllers [sender. tag];
}
# Pragma mark-hide tabbar
-(Void) isHiddenCustomTabbar :( BOOL) isHidden
{
_ TabbarView. hidden = isHidden;
}

After completing the preceding steps, set RootViewController to the Root View Controller in appdelegate:

// Set the tabbar Root View Controller
_ Delegate = [[RootViewController alloc] init];
Self. window. rootViewController = _ delegate;

Then we can implement it.

Shows the effect:

 

I hope this article will help you.

 

------ Append content:

Based on the above, the custom tabbar is displayed by default, but some second-level or third-level interfaces do not need to be displayed, so you need to call
-(Void) isHiddenCustomTabbar :( BOOL) isHidden. Direct calls cannot be called, so...

The code for calling this method is as follows:

AppDelegate * app = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[App. delegate isHiddenCustomTabbar: isHidden];

The isHidden is a Boolean variable.

Of course, this is still not perfect, because it is only hidden. When the interface returns, tabbar will be gone ......

Therefore, you also need to display the custom tabbar in the-(void) viewWillAppear :( BOOL) animated method!

 

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.