No.10 Xcode(5.1.x) UITabBarController

1.UITabBarController和UINavigationController是系统提供的两种视图控制器, 它们一定程度上代表了程序的流程结构, 他们也可以混合在一起使用, 混合时最好将UINavigationController作为UITabBarController的子控制器;

2.tabBar.translucent=NO时, 子控制器的视图不会被UITabBarController的控制栏遮挡;

3.子控制器的tabBarItem, 会作为UITabBarController的控制栏的一个项来显示. 子控制器的title属性, 与tabBarItem.title属性, 都能影响到控制栏的标题文字;

4.子控制器的tabBarItem的 [setFinishedSelectedImage: withFinishedUnselectedImage:] 方法, 在iOS7中被换成了 [initWithTitle: image: selectedImage:] 方法, iOS7中的UIImage参数需要使用 [imageWithRenderingMode:] 方法加工后, 才能显示正常;

5.子控制器的tabBarItem的选中和未选中图标, 要求尺寸是30x30, 才会正常显示;


创建UITabBarController时

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    UINavigationController* naviController0 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithTitle:@"Page0"]];
    naviController0.navigationBar.translucent = NO; // 可以换成YES测试下效果
    UINavigationController* naviController1 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithTitle:@"Page1"]];
    naviController1.navigationBar.translucent = NO;
    UINavigationController* naviController2 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithTitle:@"Page2"]];
    naviController2.navigationBar.translucent = NO;
    
    UITabBarController* tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = @[naviController0, naviController1, naviController2];
    tabBarController.tabBar.translucent = NO; // 可以换成YES测试下效果
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = tabBarController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

子控制器中

- (id)initWithTitle:(NSString*)title
{
    self = [super init];
    if (self)
    {
        // 把这段代码移到 [viewDidLoad] 中, 可以测试下效果
        UITabBarItem* tabBarItem = [[UITabBarItem alloc] init];
        tabBarItem.badgeValue    = @"xyz";  // 右上图标
        tabBarItem.title         = title;   // 标题文字
        tabBarItem.image         = [[UIImage imageNamed:@"fruit-apple0.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 选中与未选中时的图片, 尺寸必须是30x30
        tabBarItem.selectedImage = [[UIImage imageNamed:@"fruit-apple1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // iOS7中的方法
        [tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"fruit-apple0.png"]
                 withFinishedUnselectedImage:[UIImage imageNamed:@"fruit-apple1.png"]]; // iOS6以前的方法

        self.title = title;
        self.tabBarItem = tabBarItem;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.layer.borderColor = [UIColor redColor].CGColor;
    self.view.layer.borderWidth = 2.0;
    self.view.layer.masksToBounds = YES;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值