UITabBarController

1、新建ios工程,并添加几个自定义UIViewController,分别取名为MyViewController1,MyViewController2,MyViewController3,


2、AppDelegate中如下初始化

#import "AppDelegate.h"
#import "MyViewController1.h"
#import "MyViewController2.h"
#import "MyViewController3.h"
//#import "MyViewController4.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    UITabBarController* tb=[[UITabBarController alloc]init];
    UIViewController* vc1=[[MyViewController1 alloc]init];
    vc1.tabBarItem.title=@"首页";
    //避免图片不显示
    vc1.tabBarItem.image=[[UIImage imageNamed:@"img1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UIViewController* vc2=[[MyViewController2 alloc]init];
    vc2.tabBarItem.title=@"消息";
    
    UIViewController* vc3=[[MyViewController3 alloc]init];
    vc3.tabBarItem.title=@"设置";
    
    tb.viewControllers=@[vc1,vc2,vc3];
    //设置底部tab背景
    tb.tabBar.backgroundImage=[UIImage imageNamed:@"tb.png"];
    
    self.window.rootViewController=tb;
    
    [self.window makeKeyAndVisible];
    return YES;
}



3、自定义各个自定义UIViewController界面,例如

#import "MyViewController2.h"

@interface MyViewController2 ()

@end

@implementation MyViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor greenColor];
    self.tabBarItem.title=@"Vc2title";
    //设置图片
    self.tabBarItem.image=[[UIImage imageNamed:@"img1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.tabBarItem.badgeValue=@"5";//显示5条未读消息
    // Do any additional setup after loading the view.
}


效果图如下

UITabBarController_第1张图片              UITabBarController_第2张图片           

你可能感兴趣的:(ios)