UITabBar初谈

来源:互联网 发布:excel表格数据取整 编辑:程序博客网 时间:2024/06/07 05:19

    抱着对苹果产品的热爱,加上对程序员工作的向往,前一阵开始转行从事iOS开发工作。学习中发现这份工作属实不易,但好在身边的朋友都很热情,帮助我指引我逐渐对移动开发这一行业有了一定的了解。为了不在不久的未来忘记了之前学过的知识,也为了与诸位大神交流学习,遂开始了这个博客。一直在想第一篇文章应该写点什么内容,既想有深度又想有广度,最后决定偷个懒,把现在正在做的小项目的知识点拿出来写写心得体会,下面就开始吧。

    UITabbarController与UINavigationController相类似,都继承于UIViewController,都可以称为多个视图控制器的控制器,但又有很大不同。UITabbarController的控制对象是多个视图控制器的平行跳转,而UINavigationController的控制对象则类似于顺序排列。

一、UITabbarController的使用

    1.初始化UITabbarController;

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];//定义UITabbarController对象UITabBarController *rootBarC = [[UITabBarController alloc] init];//设置根视图控制器 self.window.rootViewController = rootBarC;
    2.设置多个UITabbarController的子控制器

//设置多个子控制器    HomeViewController *homeVC = [[HomeViewController alloc] init];    UINavigationController *homeNC = [[UINavigationController alloc] initWithRootViewController:homeVC];    UIImage *homeImage = [UIImage imageNamed:@"tabbar_home"];    homeNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:homeImage tag:101];        ChatViewController *chatVC = [[ChatViewController alloc] init];    UINavigationController *chatNC = [[UINavigationController alloc] initWithRootViewController:chatVC];    UIImage *chatImage = [UIImage imageNamed:@"tabbar_chat"];    chatNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"微聊" image:chatImage tag:102];        MineViewController *mineVC = [[MineViewController alloc] init];    UINavigationController *mineNC = [[UINavigationController alloc] initWithRootViewController:mineVC];    UIImage *mineImage = [UIImage imageNamed:@"tabbar_mine"];    mineNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:mineImage tag:103];        SetViewController *setVC = [[SetViewController alloc] init];    UINavigationController *setNC = [[UINavigationController alloc] initWithRootViewController:setVC];    UIImage *setImage = [UIImage imageNamed:@"tabbar_set.png"];    setNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:setImage tag:104];
    3.将子控制器添加进UITabbarController

    [rootBarC addChildViewController:homeNC];    [rootBarC addChildViewController:chatNC];    [rootBarC addChildViewController:mineNC];    [rootBarC addChildViewController:setNC];
    运行程序,就可以看到如下界面:


    功能实现了,但是tabbaritem里多图标并没有显示,这是因为从iOS7以上版本开始,系统会自动渲染点击事件的图片,怎么办,看下面。

二、去除图片的自动渲染

    在UIImage的类库中会发现下面的方法:

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);
    他是用来设置图片渲染方式的,继续点击UIImageRenderingMode会发现下面内容:

/* Images are created with UIImageRenderingModeAutomatic by default. An image with this mode is interpreted as a template image or an original image based on the context in which it is rendered. For example, navigation bars, tab bars, toolbars, and segmented controls automatically treat their foreground images as templates, while image views and web views treat their images as originals. You can use UIImageRenderingModeAlwaysTemplate to force your image to always be rendered as a template or UIImageRenderingModeAlwaysOriginal to force your image to always be rendered as an original. */typedef NS_ENUM(NSInteger, UIImageRenderingMode) {    UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used        UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information} NS_ENUM_AVAILABLE_IOS(7_0);
    这就好办了,使用上面的方法,取消图片的渲染,就可以看到设置的图片了。代码如下:

homeImage = [homeImage imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    其他图片也跟着使用上面的方法,再运行下:

    出来啦!

    注意一下,虽然renderingMode是UI Image的属性,但由于他是只读(readonly)类型的,所以要使用上面的方法哦。renderingMode


0 0
原创粉丝点击