Custom Tabbar #iOS

Source: Internet
Author: User

我们在做项目的过程中,会经常需要自定义tabbar。我今天刚好整理了一下,把自己的想法写出来和大家分享一下。
Custom Tabbar First we create a new subclass that inherits from Uitabbar Eztabbar

EzTabBar.h File Contents

Import "EzTabBar.h" #define Tabbar_inset @implementation eztabbar-(void) Initialize {Uiimageview * barv = [[Uiimageview Alloc]init]; Self.barimage = Barv; Self.barImage.frame = CGRectMake (tabbar_inset, 0, screenwidth/4.0f-(2 * tabbar_inset), 3); Self.barImage.backgroundCol or = COLOR (210,0,12,1); [Self addSubview:self.barImage];} -(ID) Initwithcoder: (Nscoder *) Adecoder {self = [super Initwithcoder:adecoder], if (self) {[self initialize];} return SE Lf }-(ID) initWithFrame: (CGRect) frame {self = [super Initwithframe:frame], if (self) {[self initialize];} return to self;} -(Cgsize) Sizethatfits: (cgsize) Size {cgsize sizethatfits = [Super Sizethatfits:size]; sizethatfits.height = 44;return Sizethatfits;} -(void) layoutsubviews {[Super layoutsubviews]; [Self bringSubviewToFront:self.barImage]; }-(void) Setselecteditem: (Uitabbaritem *) SelectedItem {[Super Setselecteditem:selecteditem]; Nsinteger Indexofselecteditem = [[Self items] indexofobject:selecteditem]; [UIView animatewithduration:.3 delay:.0 options:uiviewanimationoptioncurveeaseinout animations:^{self.barImage.frame = CGRectMake (Tabbar_inset + indexofselecteditem* (screenwidth/4.0f), 0, Self.barImage.frame.size.width, 3); } completion:^ (BOOL finished) {}]; } @end Custom Tabbarviewcontrollerimport "EzTabBarController.h" @interface Eztabbarcontroller () @end @implementation Eztabbarcontroller (ID) initwithnibname: (nsstring) Nibnameornil Bundle: (NSBundle) Nibbundleornil {self = [super] Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self; } (void) viewdidload {[Super viewdidload]; Uitabbar *tabbar = Self.tabbar; Uitabbaritem *tabbaritem1 = [Tabbar.items objectatindex:0]; Uitabbaritem *tabbaritem2 = [Tabbar.items objectatindex:1]; Uitabbaritem *tabbaritem3 = [Tabbar.items objectatindex:2]; Uitabbaritem *tabbaritem4 = [Tabbar.items objectatindex:3];tabbaritem1.image = [UIImage imagenamed:@ "TabBarIconHome" ] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; Tabbaritem1.selectedImage = [[UIImage imagenamed:@ "tabbariconhomeselected"] Imagewithrenderingmode: Uiimagerenderingmodealwaysoriginal];tabbaritem2.image = [[UIImage imagenamed:@ "Tabbariconwardrobe"] Imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; Tabbaritem2.selectedimage = [[UIImage imagenamed:@ "tabbariconwardrobeselected"] Imagewithrenderingmode: Uiimagerenderingmodealwaysoriginal];tabbaritem3.image = [[UIImage imagenamed:@ "Tabbariconmyshop"] Imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; Tabbaritem3.selectedimage = [[UIImage imagenamed:@ "tabbariconmyshopselected"] Imagewithrenderingmode: Uiimagerenderingmodealwaysoriginal];tabbaritem4.image = [[UIImage imagenamed:@ "Tabbariconpersonal"] Imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; Tabbaritem4.selectedimage = [[UIImage imagenamed:@ "tabbariconpersonalselected"] Imagewithrenderingmode: Uiimagerenderingmodealwaysoriginal];self.view.backgroundcolor = [Uicolor whitecolor];} -(void) Viewdidappear: (BOOL) animated{[suPer viewdidappear:animated]; } #pragma mark-segues-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender {[Super Prepareforsegue: Segue Sender:sender]; if ([Segue.identifier isequaltostring:@ "Switchtomyshopsegue"]) {[Segue.destinationviewcontroller setUserId:@ "0"];} else if ([Segue.identifier isequaltostring:@ "Baseoptionssegue"]) {(baseoptionsforcreationviewcontroller*) Segue.destinationviewcontroller). Isfromfloatingbutton = YES; }} @end

  

Custom Tabbar #iOS

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.