iOS Tabbar和Naviagtion

1、新建一个继承于UITabbarViewController 的类,将该类命名为RootTabBarViewController。然后,新建三个继承于UIViewController的类,分别命名为AViewController、BViewController、CViewController。

2、在RootTabBarViewController中添加如下代码:

#import "RootTabBarViewController.h"

#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"

@interface RootTabBarViewController ()

@end

@implementation RootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AViewController *aVC = [[AViewController alloc] init];
    BViewController *bVC = [[BViewController alloc] init];
    CViewController *cVC = [[CViewController alloc] init];

    aVC.title = @"第一个";
    bVC.title = @"第二个";
    cVC.title = @"第三个";

    UINavigationController *navgation1 = [[UINavigationController alloc] initWithRootViewController:aVC];
    UINavigationController *navgation2 = [[UINavigationController alloc] initWithRootViewController:bVC];
    UINavigationController *navgation3 = [[UINavigationController alloc] initWithRootViewController:cVC];

    self.viewControllers = @[navgation1,navgation2,navgation3];

}

@end

3、在AViewController、BViewController 、CViewController 中添加如下代码:

#import "AViewController.h"

@interface AViewController ()

@end

@implementation AViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"首页"
                                                           image:[UIImage imageNamed:@"tabbar1"]
                                                   selectedImage:[UIImage imageNamed:@"tabbar1_a"]];

        self.tabBarItem = item;
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"应用"
                                                           image:[UIImage imageNamed:@"tabbar2"]
                                                   selectedImage:[UIImage imageNamed:@"tabbar2_a"]];

        self.tabBarItem = item;
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"应用"
                                                           image:[UIImage imageNamed:@"tabbar3"]
                                                   selectedImage:[UIImage imageNamed:@"tabbar3_a"]];

        self.tabBarItem = item;
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

注意:此方法只设置图片,对于设置标题并不起作用。

iOS Tabbar和Naviagtion_第1张图片

你可能感兴趣的:(ios)