你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS - tabbar 嵌套 navigation(二)

iOS - tabbar 嵌套 navigation(二)

編輯:關於IOS

(1)創建一個繼承UINavigationController的類BaseNavigationViewController

    [objc]   #import <UIKit/UIKit.h>      @interface BaseNavigationViewController : UINavigationController      @end     [objc] view plaincopy在CODE上查看代碼片派生到我的代碼片 - (void)viewDidLoad   {       [super viewDidLoad];       // Do any additional setup after loading the view.          //    設置navigationBar的顏色       if ([self.navigationBar respondsToSelector:@selector(setBackButtonBackgroundImage:forState:barMetrics:)]){                  [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault];              }                }             (2)創建5個控制器類,把那個繼承導航控制器的類和這5個類,把它們的頭文件引入到MainViewController(繼承的是UITabBarController )     [objc]   #import "HomeViewController.h"   #import "NewsViewController.h"   #import "FriendsViewController.h"   #import "PlazaViewController.h"   #import "MoreViewController.h"   #import "BaseNavigationViewController.h"           (3)MainViewController.m   [objc]   //初始化view controller   -(void) _initViewController {              HomeViewController *homeVC = [[HomeViewController alloc]init];       NewsViewController *newsVC = [[NewsViewController alloc]init];       FriendsViewController *friendsVC = [[FriendsViewController alloc] init];       PlazaViewController *plazaVC = [[PlazaViewController alloc] init];       MoreViewController *moreVC = [[MoreViewController alloc]init];              NSArray *array = @[homeVC,newsVC,friendsVC,plazaVC,moreVC];              NSMutableArray *viewControllerArray = [NSMutableArray arrayWithCapacity:5];              for (UIViewController *viewController in array) {           BaseNavigationViewController *nav = [[BaseNavigationViewController alloc]initWithRootViewController:viewController];           [viewControllerArray addObject:nav];       }              self.viewControllers = viewControllerArray;          }     [objc]  //自定義tabbar   -(void) _initTabbarView {              _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 49, 320, 49)];   //    _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 49 -20, 320, 49)];       _tabbarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background.png"]];       [self.view addSubview:_tabbarView];              NSArray *backgroundImage = @[@"tabbar_home.png",@"tabbar_message_center.png",@"tabbar_profile.png",@"tabbar_discover.png",@"tabbar_more.png"];       NSArray *heightBackgroundImage =@[@"tabbar_home_highlighted.png",@"tabbar_message_center_highlighted.png",@"tabbar_profile_highlighted.png",@"tabbar_discover_highlighted.png",@"tabbar_more_highlighted.png"];       NSArray *titleArray = @[@"首頁",@"消息",@"好友",@"廣場",@"更多"];              for (int i = 0; i < [backgroundImage count]; i ++ ) {                      NSString *backImage = backgroundImage[i];           NSString *heightImage = heightBackgroundImage [i];                      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];           button.frame = CGRectMake((320/5 - 30)/2 + (64*i), (49 - 30)/2, 30, 40);           button.tag = i;           forState:UIControlStateNormal];           forState:UIControlStateHighlighted];           button.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, button.titleLabel.bounds.size.width);//設置image在button上的位置(上top,左left,下bottom,右right)這裡可以寫負值,對上寫-5,那麼image就象上移動5個像素                                 NSString *title = [titleArray objectAtIndex:i];           ;           button.titleLabel.font = [UIFont systemFontOfSize:10];           button.titleLabel.textAlignment = NSTextAlignmentCenter;//設置title的字體居中           forState:UIControlStateNormal];//設置title在一般情況下為白色字體           forState:UIControlStateHighlighted];//設置title在button被選中情況下為灰色字體           button.titleEdgeInsets = UIEdgeInsetsMake(20, -button.titleLabel.bounds.size.width-30, 0, 0);//設置title在button上的位置(上top,左left,下bottom,右right)                                          //        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;                      ;           [_tabbarView addSubview:button];                         }       }      -(void)tap:(UIButton *)button {      //    NSLog(@"tap a button");              self.selectedIndex = button.tag;//讓button有一個tabbar控制器的功能                    }     [objc]   - (void)viewDidLoad   {       [super viewDidLoad];       // Do any additional setup after loading the view.              [self.tabBar setHidden:YES];//隱藏系統的tabbar              [self _initViewController];       [self _initTabbarView];   }       (4)AppDelegate   [objc]   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions   {       self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];       // Override point for customization after application launch.       self.window.backgroundColor = [UIColor whiteColor];              _mainVC= [[MainViewController alloc] init];              self.window.rootViewController = _mainVC;                          [self.window makeKeyAndVisible];       return YES;   }    
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved