快捷搜索:

思路就是1.移除原来的tabbar

最终效果:

图片 1最终效果

思路就是1.移除原来的tabbar,自定义一个view2.覆盖原来的tabbar

======华丽的分割线====演示覆盖1.拉一个Tab bar Controller进storyboard

图片 2图1

2.command N创建文件继承自UITarbarController3.command N在创建继承自UIView如下图所示

图片 3我就是如下图4.在Controller中加入view,并且加入UIButton

#import "PLTabBarController.h"#import "TabBarView.h"@interface PLTabBarController ()<TabBarViewDelegate>@end@implementation PLTabBarController- viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. TabBarView * tabbar = [[TabBarView alloc]init]; tabbar.frame = self.tabBar.bounds; tabbar.delegate = self; NSString * imageName = nil; NSString * selName = nil; for (int i = 0; i < self.childViewControllers.count; i  ) { imageName = [NSString stringWithFormat:@"%d",i 1]; selName = [NSString stringWithFormat:@"s%d",i 1]; [tabbar addTarBarButtonWithName:imageName selName:selName]; } [self.tabBar addSubview:tabbar]; }- tabbar:(TabBarView *)tabbar didSelectedIndex:(NSInteger)selectIndex{ self.selectedIndex = selectIndex;}@end

5.UIButton实现点击效果,代理切换控制器.h文件

#import <UIKit/UIKit.h>@class TabBarView;@protocol TabBarViewDelegate<NSObject>@optional- tabbar:(TabBarView*)tabbar didSelectedIndex:(NSInteger)selectIndex;@end@interface TabBarView : UIView@property (nonatomic, weak) id <TabBarViewDelegate> delegate;- addTarBarButtonWithName:(NSString *)name selName:(NSString *)selName;@end

.m

#import "TabBarView.h"@interface TabBarView ()@property (nonatomic, weak) UIButton *selectedBtn;@end@implementation TabBarView- (instancetype)initWithFrame:frame{ self = [super initWithFrame:frame]; if  { } return self;}- addTarBarButtonWithName:(NSString *)name selName:(NSString *)selName{ UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected]; [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchDown]; [self addSubview:button];}- btnClicked:(UIButton *)button{ self.selectedBtn.selected = NO; button.selected = YES; self.selectedBtn = button; if ([self.delegate respondsToSelector:@selector(tabbar:didSelectedIndex:)]) { [self.delegate tabbar:self didSelectedIndex:button.tag]; }}- layoutSubviews{ [super layoutSubviews]; CGFloat btnX = 0; CGFloat btnY = 0; CGFloat btnW = self.frame.size.width /self.subviews.count; CGFloat btnH = self.frame.size.height; for (int i = 0; i < self.subviews.count; i  ) { UIButton * btn = self.subviews[i]; btn.tag = i; btnX = i * btnW; [btn setFrame:CGRectMake(btnX, btnY, btnW, btnH)]; if  { [self btnClicked:btn]; } }}@end

本文由澳门新葡萄京8455官网发布于澳门新葡萄京8455官网,转载请注明出处:思路就是1.移除原来的tabbar

您可能还会对下面的文章感兴趣: