现在的位置: 首页 > 综合 > 正文

iOS 自定义tabbar

2019年03月31日 ⁄ 综合 ⁄ 共 5066字 ⁄ 字号 评论关闭

建议先参考http://www.th7.cn/Program/IOS/201306/141129.shtml

自己建一个类继承自uitabbarcontroller

在类里实现

- (void)viewDidLoad

{

    [super
viewDidLoad];

    NSMutableArray*arr = [NSMutableArray
arrayWithObjects:@"dibu_tubiao_1.png",@"dibu_tubiao_2.png",@"dibu_tubiao_3.png",
@"dibu_tubiao_4.png",@"dibu_tubiao_5.png",@"dibu_tubiao_6.png",nil];

    NSMutableArray*labels = [NSMutableArray
arrayWithObjects:@"价格行情",@"分析预测",@"优质供应商",@"应季农产品",@"食品安全",@"食材食谱",nil];

    

UIImageView *tarBarView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
iPhoneHeight-49,
320
, 49)];

    tarBarView.userInteractionEnabled =
YES;

    tarBarView.image = [UIImage
imageNamed:@"ios-Route-way_black_bg_1.png"];

    [self.view
addSubview
:tarBarView];

    

    if (iOS7&&iPhone5) {

        //        tarBarView.frame = CGRectMake(0, , <#CGFloat width#>, <#CGFloat height#>)

    }else if (iOS7&&!iPhone5){

        

    }else if (!iOS7&&iPhone5){

        

    }else if (!iOS7&&iPhone5){

        

    }

    

    scrolbutton = [[UIScrollView
alloc]initWithFrame:CGRectMake(0,
0, 320-64,
49)];

    scrolbutton.contentSize =
CGSizeMake(320+64,
49);

    scrolbutton.userInteractionEnabled =
YES;

    scrolbutton.pagingEnabled =
YES;

    scrolbutton.delegate =
self;

//    scrolbutton.scrollEnabled = NO;

    scrolbutton.showsHorizontalScrollIndicator =
NO;

    scrolbutton.showsVerticalScrollIndicator =
NO;

    UIImageView*iamgeview =  [[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"ios-Route-way_black_bg_1.png"]];

    [scrolbutton addSubview:iamgeview];

    [tarBarView addSubview:scrolbutton];

    [iamgeview release];

    

    for (int i =
0
; i<6; i++) {

        UIView* buttonlabel = [[UIView
alloc]init];

        buttonlabel.frame =
CGRectMake
(64*i, 0,
64
, 49);

        buttonlabel.backgroundColor = [UIColor
clearColor];

        

        UIImageView*imageview = [[UIImageView
alloc]init];

        [imageview setImage:[UIImage
imageNamed:[arr objectAtIndex:i]]];

        imageview.frame =
CGRectMake
(10, 2,
44
, 33);

        

        UILabel*label = [[UILabel
alloc]init];

        label.frame = CGRectMake(0,
37, 64, 10);

        label.backgroundColor = [UIColor
clearColor];

        label.font = [UIFont
fontWithName:@"Arial"
size
:12];

        label.textAlignment =
UITextAlignmentCenter;

        label.textColor = [UIColor
whiteColor];

        label.text = [labels
objectAtIndex
:i];

        

        UIButton*button = [[UIButton
alloc]init];

        button.frame = CGRectMake(0,
0, 64, 49);

        button.tag = i;

        button.backgroundColor = [UIColor
clearColor];

        [button addTarget:self
action:@selector(changeViewController:)
forControlEvents:UIControlEventTouchUpInside];

        

        

        [buttonlabel addSubview:imageview];

        [buttonlabel addSubview:label];

        [buttonlabel addSubview:button];

        [scrolbutton addSubview:buttonlabel];

        [buttonlabel release];

        [label release];

        [imageview release];

    }

    

    UIView * viewoffet = [[UIView
alloc]init];

    viewoffet.frame = CGRectMake(320-64,
0, 64, 49);

    viewoffet.userInteractionEnabled =
YES;

    viewoffet.backgroundColor = [UIColor
clearColor];

    jiantou = [[UIImageView
alloc]init];

    jiantou.frame =
CGRectMake(17, 12,
30, 25);

    [jiantou
setImage:[UIImage
imageNamed:@"arrow_left.png"]];

    

    UIButton* buttonoffet = [[UIButton
alloc]init];

    buttonoffet.frame  = CGRectMake(0,
0, 64, 49);

    buttonoffet.backgroundColor = [UIColor
clearColor];

    [buttonoffet addTarget:self
action:@selector(changeoffet)
forControlEvents:UIControlEventTouchUpInside];

    

    [viewoffet addSubview:jiantou];

    [viewoffet addSubview:buttonoffet];

    [tarBarView addSubview:viewoffet];

    [buttonoffet release];

    [viewoffet release];

    //

    //

    //

    //    self.tabBar.backgroundImage = [UIImage imageNamed:@"ios-Route-way_black_bg_1.png"];

    //

    //    float coordinax = 0;

    //    for (int index = 0 ; index < 6 ; index ++) {

    ////        UIView   * buttonview = [UIView alloc]initWithFrame:CGRectMake(coordinax, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)

    //        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //        button.tag = index;

    //        button.frame = CGRectMake(coordinax, 49.0/2-10, 25, 25);

    //

    //        NSString *imageName = [NSString stringWithFormat:@"%d",index+1];

    //        [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

    //        [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];

    //        [self.tabBar addSubview:button];

    //        coordinax +=64;

    //    }

    //    [tarBarView release];

}

-(void)changeViewController:(id) sender{

    UIButton *button = (UIButton *) sender;

    self.selectedIndex = button.tag;

}

- (void)changeoffet{

    if (flag ==
NO
) {

        [jiantou
setImage:[UIImage
imageNamed:@"arrow_right.png"]];

        [UIView
beginAnimations:nil
context:nil];

        [UIView
setAnimationDuration:0.3];

        scrolbutton.contentOffset =
CGPointMake(128,
0
);

        [UIView
commitAnimations];

        flag = YES;

    }else{

        [jiantou
setImage:[UIImage
imageNamed:@"arrow_left.png"]];

        [UIView
beginAnimations:nil
context:nil];

        [UIView
setAnimationDuration:0.3];

        scrolbutton.contentOffset =
CGPointMake(0, 0);

        [UIView
commitAnimations];

        flag = NO;

    }

}

#pragma mark - 滑动scrollview调用的方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSLog(@"滑动了");

    if (scrollView == scrolbutton) {

        if (scrolbutton.contentOffset.x ==
0) {

            [jiantou setImage:[UIImage
imageNamed:@"arrow_left.png"]];

            

        }else if(scrolbutton.contentOffset.x >=
128){

            [jiantou setImage:[UIImage
imageNamed:@"arrow_right.png"]];

            

        }

    }

}

点击右边的箭头可以实现菜单栏的滑动,菜单栏也可以手动滑动

抱歉!评论已关闭.