iOS Quick custom Tabbar controls within Uitabbarcontroller

Source: Internet
Author: User
Tags uikit

Idea: 1. Define a Basetabbarviewcontroller class to inherit from Uitabbarcontroller

2. Hide the original tabbar, customize a control to cover the above

3. Add buttons within the control that can be clicked to adjust the details of the controls and buttons

The specific code is as follows:
. h Li
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "HZSButton.h"
@interface basetabbarviewcontroller:uitabbarcontroller<uitabbarcontrollerdelegate>
{
Uiimageview *_tabbarview; Customize the control that overrides the original TarBar
Hzsbutton *_previousbtn; Record the previous selected button
}
@end



. M-Li
#import "BaseTabBarViewController.h"

@interface Basetabbarviewcontroller ()

@end

@implementation Basetabbarviewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view.

I want to invest
Firstviewcontroller *afirstviewcontroller=[[firstviewcontroller Alloc]init];
Uinavigationcontroller *nav0=[[uinavigationcontroller Alloc]initwithrootviewcontroller:afirstviewcontroller];



My Account
Secondviewcontroller *asecondviewcontroller=[[secondviewcontroller Alloc]init];
Uinavigationcontroller *nav1=[[uinavigationcontroller Alloc]initwithrootviewcontroller:asecondviewcontroller];


More
Thirdviewcontroller *athirdviewcontroller=[[thirdviewcontroller Alloc]init];
Uinavigationcontroller *nav2=[[uinavigationcontroller Alloc]initwithrootviewcontroller:athirdviewcontroller];

Self.viewcontrollers=[nsarray Arraywithobjects:nav0,nav1,nav2,nil];


self.delegate=self;


Self.tabBar.hidden = YES; Hide the original Tabbar
CGFloat tabbarviewy = self.view.frame.size.height-49;
_tabbarview = [[Uiimageview alloc] Initwithframe:cgrectmake (0, Tabbarviewy, Self.view.frame.size.width, 49)];
_tabbarview.userinteractionenabled = YES; This step must be set to Yes, otherwise you cannot interact with the user
_tabbarview.image = [UIImage imagenamed:@ "[email protected]"];
_tabbarview.backgroundcolor=[uicolor colorwithred:232.0/255.0 green:235.0/255.0 blue:240.0/255.0 alpha:1];
[Self.view Addsubview:_tabbarview];

The following method is the method that invokes the custom build button
[Self creatbuttonwithnormalname:@ "[email protected]" andselectname:@ "[email protected]" andtitle:@ "message" andindex:0 ";
[Self creatbuttonwithnormalname:@ "[email protected]" andselectname:@ "[email protected]" andtitle:@ "Contacts" andindex:1];
[Self creatbuttonwithnormalname:@ "[email protected]" andselectname:@ "[email protected]" andtitle:@ "my" andindex:2 ";

Hzsbutton *btn = (Hzsbutton *) [_tabbarview viewwithtag:8888];
[Self changeviewcontroller:btn]; The button in the custom control is clicked on the calling method, and the first button is selected by default into the interface
}


Create a button
-(void) Creatbuttonwithnormalname: (NSString *) normal andselectname: (NSString *) selected Andtitle: (NSString *) title Andindex: (int) index
{
/*

Hzsbutton is a custom class that inherits from the UIButton, and the purpose of customizing the class is because the system comes with a button that can set the image and title properties, but the default image is on the left side of the title, so if you think about the image above, Put the image on top of the title, you need to customize the button, set something. (The specific Hzsbutton set what, put in the following talk)
*/

CGFloat buttonw = _TABBARVIEW.FRAME.SIZE.WIDTH/3;
CGFloat buttonh = _tabbarview.frame.size.height;


UIView *aview=[[uiview Alloc]init];
Aview.backgroundcolor=[uicolor colorwithred:46.0/255.0 green:167.0/255.0 blue:224.0/255.0 alpha:1];
Aview.frame = CGRectMake (buttonw *index+buttonw/2.0-17.5, 0, Buttonh);
[_tabbarview Addsubview:aview];

Hzsbutton *button = [Hzsbutton buttonwithtype:uibuttontypecustom];
Button.tag = index+8888;
Button.frame = CGRectMake (buttonw *index, 0, buttonw, BUTTONH);
[Button Setimage:[uiimage Imagenamed:normal] forstate:uicontrolstatenormal];
[Button Setimage:[[uiimage imagenamed:selected] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal] Forstate:uicontrolstatedisabled];
[Button Settitle:title Forstate:uicontrolstatenormal];
[Button addtarget:self action: @selector (Changeviewcontroller:) forcontrolevents:uicontroleventtouchupinside];
Button.imageView.contentMode = Uiviewcontentmodecenter; Center The picture inside the button
Button.titleLabel.textAlignment = Nstextalignmentcenter; Center the caption inside the button
Button.titleLabel.font = [Uifont systemfontofsize:12]; Set the font size for headings
[Button Settitlecolor:[uicolor colorwithred:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1] ForState: UIControlStateNormal];
[Button Settitlecolor:[uicolor colorwithred:242.0/255.0 green:132.0/255.0 blue:44.0/255.0 alpha:1] ForState: Uicontrolstatedisabled];
[_tabbarview Addsubview:button];
}


Called when the button is clicked
-(void) Changeviewcontroller: (Hzsbutton *) sender
{
Self.selectedindex = sender.tag-8888; Switching between interfaces of different controllers

Self.tabbarcontroller.selectedviewcontroller=[self.tabbarcontroller.viewcontrollers ObjectAtIndex: Self.selectedindex];
sender.enabled = NO;
if (_previousbtn! = Sender) {

_previousbtn.enabled = YES;

}

_PREVIOUSBTN = sender;
NSLog (@ "Self.selectedindex%ld", Self.selectedindex);


}

iOS Quick custom Tabbar controls within Uitabbarcontroller

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.