依赖于第三方的框架RESideMenu
1.AppDelegate.m中的实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //在AppDelegate.h中声明属性,初始化tabBarController self.tabBar = [[UITabBarController alloc] init]; ViewController* vc1 = [[ViewController alloc] init]; vc1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0]; UINavigationController* nav1 = [[UINavigationController alloc] initWithRootViewController:vc1]; ViewController1* vc2 = [[ViewController1 alloc] init]; vc2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1]; UINavigationController* nav2 = [[UINavigationController alloc] initWithRootViewController:vc2]; self.tabBar.viewControllers = @[nav1,nav2]; //初始化leftVC LeftController* left = [[LeftController alloc] init]; //初始化RESideMenu RESideMenu* menu = [[RESideMenu alloc] initWithContentViewController:self.tabBar leftMenuViewController:left rightMenuViewController:nil]; self.window.rootViewController= menu; return YES; }
2.left左侧菜单
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ AppDelegate* GHDelegate = (AppDelegate* )[UIApplication sharedApplication].delegate; ViewController1_1* vc = [[ViewController1_1 alloc] init]; //通过GHDelegate.tabBar.selectedIndex获得当前tabbaritem对应的nav,进行页面跳转 NSArray *arrControllers = GHDelegate.tabBar.viewControllers; if (GHDelegate.tabBar.selectedIndex==0) { UINavigationController* nav = (UINavigationController* )[arrControllers objectAtIndex:0]; //隐藏sideMenuViewController [self.sideMenuViewController hideMenuViewController]; //隐藏底部 vc.hidesBottomBarWhenPushed = YES; [nav pushViewController:vc animated:YES]; }else{ UINavigationController* nav = (UINavigationController* )[arrControllers objectAtIndex:1]; [self.sideMenuViewController hideMenuViewController]; vc.hidesBottomBarWhenPushed = YES; [nav pushViewController:vc animated:YES]; } }
3.RESideMenu常见的属性设置
- (void)awakeFromNib { self.parallaxEnabled = NO; //视图差效果 self.scaleContentView = YES; //中心视图缩放功能打开 self.contentViewScaleValue = 0.95; //侧滑出现时缩放比 self.scaleMenuView = NO; //侧滑出来的视图是否支持缩放 self.contentViewShadowEnabled = YES; //中心视图阴影效果,打开显得有层次感。 self.contentViewShadowRadius = 4.5; //中心视图阴影效果Radius self.panGestureEnabled = NO; //关闭拖动支持手势 //使用storyboard初始化中心视图和左视图。 self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentViewController"]; //tabbar controller self.leftMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"leftMenuViewController"]; }
附件:
4.设置基本导航控制器
#import "LHLNavigationViewController.h" @interface LHLNavigationViewController () <UIGestureRecognizerDelegate> @end @implementation LHLNavigationViewController + (void)load { UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil]; // 设置导航条字体 NSMutableDictionary *attr = [NSMutableDictionary dictionary]; attr[NSFontAttributeName] = [UIFont systemFontOfSize:20]; [navBar setTitleTextAttributes:attr]; // 设置背景图片 [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault]; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.childViewControllers.count > 0) { // 非根控制器
// 自定义UIBarButtonItem的分类方法 UIBarButtonItem *item = [UIBarButtonItem backItemWitnImage:[UIImage imageNamed:@"navigationButtonReturn"] heighlightImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(back) title:@"返回"]; viewController.hidesBottomBarWhenPushed = YES; viewController.navigationItem.leftBarButtonItem = item; } [super pushViewController:viewController animated:animated]; } - (void)back { [self popViewControllerAnimated:YES]; } - (void)viewDidLoad { [super viewDidLoad];
// 全屏返回手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)]; pan.delegate = self; self.interactivePopGestureRecognizer.enabled = NO; [self.view addGestureRecognizer:pan]; } /* <UIScreenEdgePanGestureRecognizer: 0x7f7f98c97430; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7f7f98f19d00>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7f7f98c96ee0>)> */ #pragma mark - UIGestureRecognizerDelegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return self.childViewControllers.count > 1; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
5.设置UITabBarViewController
#import "LHLTabBarController.h" #import "LHLEssenceViewController.h" #import "LHLFriendTrendViewController.h" #import "LHLMeViewController.h" #import "LHLNewViewController.h" #import "LHLPublishViewController.h" #import "LHLTabBar.h" #import "LHLNavigationViewController.h" @interface LHLTabBarController () @end @implementation LHLTabBarController // load方法只会调用一次 + (void)load { // 设置某个类中的 UITabBarItem 按钮的颜色 UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil]; NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSForegroundColorAttributeName] = [UIColor blackColor]; [item setTitleTextAttributes:attrs forState:UIControlStateSelected]; // 设置字体的尺寸:只有正常状态下设置才有效果 NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary]; attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13]; [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal]; } #pragma mark - 初始化 - (void)viewDidLoad { [super viewDidLoad]; // 1.创建所有子控制器 [self setUpAllChildControllers]; // 2.2设置tabBar按钮的标题 [self setUpAllTitles]; // 3.使用自定义tabBar [self setUpTabBar]; } #pragma mark - 自定义方法 - (void)setUpAllChildControllers { // 2.1.设置tabBar的子控制器 // 精华 LHLEssenceViewController *essenceVC = [[LHLEssenceViewController alloc] init]; LHLNavigationViewController *nav = [[LHLNavigationViewController alloc] initWithRootViewController:essenceVC]; [self addChildViewController:nav]; // 新帖 LHLNewViewController *newVC = [[LHLNewViewController alloc] init]; LHLNavigationViewController *nav1 = [[LHLNavigationViewController alloc] initWithRootViewController:newVC]; [self addChildViewController:nav1]; // 关注 LHLFriendTrendViewController *trendVC = [[LHLFriendTrendViewController alloc] init]; LHLNavigationViewController *nav3 = [[LHLNavigationViewController alloc] initWithRootViewController:trendVC]; [self addChildViewController:nav3]; // 我 LHLMeViewController *meVC = [[LHLMeViewController alloc] init]; LHLNavigationViewController *nav4 = [[LHLNavigationViewController alloc] initWithRootViewController:meVC]; [self addChildViewController:nav4]; } - (void)setUpAllTitles { // 精华 UINavigationController *nav = self.childViewControllers[0]; [self setTitleButton:nav title:@"精华" image:@"tabBar_essence_icon" selectImage:@"tabBar_essence_click_icon"]; // 新帖 UINavigationController *nav1 = self.childViewControllers[1]; [self setTitleButton:nav1 title:@"新帖" image:@"tabBar_new_icon" selectImage:@"tabBar_new_click_icon"]; // 关注 UINavigationController *nav3 = self.childViewControllers[2]; [self setTitleButton:nav3 title:@"关注" image:@"tabBar_friendTrends_icon" selectImage:@"tabBar_friendTrends_click_icon"]; // 我 UINavigationController *nav4 = self.childViewControllers[3]; [self setTitleButton:nav4 title:@"我" image:@"tabBar_me_icon" selectImage:@"tabBar_me_click_icon"]; } - (void)setTitleButton:(UINavigationController *)nav title:(NSString *)aTitle image:(NSString *)aImage selectImage:(NSString *)aSelectImage { nav.tabBarItem.title = aTitle; nav.tabBarItem.image = [UIImage imageRenderOriginalWithName:aImage]; nav.tabBarItem.selectedImage = [UIImage imageRenderOriginalWithName:aSelectImage]; } - (void)setUpTabBar { LHLTabBar *tabBar = [[LHLTabBar alloc] init]; // 利用KVC对系统的tabBar进行赋值: KVC的原理是通过访问属性进行赋值,不是通过setter方法进行赋值 [self setValue:tabBar forKeyPath:@"tabBar"]; } @end
6.分类文件
UIBarButtonItem
#import "UIBarButtonItem+item.h" @implementation UIBarButtonItem (item) + (UIBarButtonItem *)itemWitnImage:(UIImage *)aImage heighlightImage:(UIImage *)aSelectImage target:(id)aTarget action:(SEL)aAction { UIButton *btn = [[UIButton alloc] init]; [btn setImage:aImage forState:UIControlStateNormal]; [btn setImage:aSelectImage forState:UIControlStateHighlighted]; [btn sizeToFit]; [btn addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside]; // 添加的代码 UIView *itemView = [[UIView alloc] initWithFrame:btn.bounds]; [itemView addSubview:btn]; return [[UIBarButtonItem alloc] initWithCustomView:itemView]; } + (UIBarButtonItem *)itemWitnImage:(UIImage *)aImage selectedImage:(UIImage *)aSelectImage target:(id)aTarget action:(SEL)aAction { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setImage:aImage forState:UIControlStateNormal]; [btn setImage:aSelectImage forState:UIControlStateSelected]; [btn sizeToFit]; [btn addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside]; UIView *containerView = [[UIView alloc] initWithFrame:btn.bounds]; [containerView addSubview:btn]; return [[UIBarButtonItem alloc] initWithCustomView:containerView]; } + (UIBarButtonItem *)backItemWitnImage:(UIImage *)aImage heighlightImage:(UIImage *)aSelectImage target:(id)aTarget action:(SEL)aAction title:(NSString *)aTitle { UIButton *btn = [[UIButton alloc] init]; [btn setImage:aImage forState:UIControlStateNormal]; [btn setImage:aSelectImage forState:UIControlStateHighlighted]; [btn setTitle:aTitle forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; btn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0); [btn sizeToFit]; [btn addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside]; UIView *containerView = [[UIView alloc] initWithFrame:btn.bounds]; [containerView addSubview:btn]; return [[UIBarButtonItem alloc] initWithCustomView:containerView]; } @end
UIImae
#import "UIImage+image.h" @implementation UIImage (image) // 在周边加一个边框为1的透明像素 - (UIImage *)imageAntialias { CGFloat border = 1.0f; CGRect rect = CGRectMake(border, border, self.size.width-2*border, self.size.height-2*border); UIImage *img = nil; UIGraphicsBeginImageContext(CGSizeMake(rect.size.width,rect.size.height)); [self drawInRect:CGRectMake(-1, -1, self.size.width, self.size.height)]; img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIGraphicsBeginImageContext(self.size); [img drawInRect:rect]; UIImage* antiImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return antiImage; } // 返回一张没有被渲染的图片 + (UIImage *)imageRenderOriginalWithName:(NSString *)imageName { UIImage *image = [UIImage imageNamed:imageName]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; return image; } @end
7.UITabBar
#import "LHLTabBar.h" @interface LHLTabBar () @property (nonatomic, weak) UIButton *plusButton; @end @implementation LHLTabBar - (UIButton *)plusButton{ if (_plusButton == nil) { UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom]; [plusButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal]; [plusButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted]; [plusButton sizeToFit]; [self addSubview:plusButton]; _plusButton = plusButton; } return _plusButton; } - (void)layoutSubviews { [super layoutSubviews]; // 布局子控件 NSInteger count = self.items.count + 1; CGFloat btnW = self.lhl_width / count; CGFloat btnH = self.lhl_height; NSInteger i = 0; for (UIButton *tabBarButton in self.subviews) { // 取出UITabBarButton if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) { if (i == 2) { i += 1; } tabBarButton.frame = CGRectMake(btnW * i, 0, btnW, btnH); i++; } // 隐藏tabBar黑线 NSString *subFrames = NSStringFromCGRect(tabBarButton.frame); NSString *blackLine = @"{{0, -0.5}, {375, 0.5}}"; if ([subFrames isEqualToString:blackLine]) { tabBarButton.hidden = YES; } } // plusButton self.plusButton.center = CGPointMake(self.lhl_width * 0.5, self.frame.size.height * 0.5); } @end