zoukankan      html  css  js  c++  java
  • UITabbar 自定义

    自定义tabar,看了很多网上的例子, 和我要做的效果不一样,一直没有找到合适的.看了一篇How to Customize UITabBar on iOS 5效果非常的好.

    Building the new version of the app Blocos de Rua I was challenged to customize the UITabBar so it meets what the designer wants. In iOS 5 this is pretty easy to do but I haven’t figured out the proper way to do it from the beginning, this post is my findings on how to do it properly by correctly using the new iOS 5 APIs to customize appearance.

    The final look:

    Keep reading to see the code.

    The appearance APIs in iOS 5 are great. They reduce lots of custom drawRect: that used to be necessary to customize the default UIKit components. The first time I tried to customized the tab bar I had some problems with images been offset upwards because I was using the wrong methods. First thing I learned, the setFinishedSelectedImage:finishedUnselectedImage: from UITabBarItem is the tab’s icon not a image for the whole tab with background, icon and label.

    Customize the UITabBar is a peace of cake when you understand how the APIs should be used, take a look:

    From inside out, the UITabBar

    First - usually in your app delegate - set the image for the entire tab bar’s background, which represents the “normal” state of all tabs. The dimension is 320 x 49 points.

    1
    
    [[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"background"]];
    

    Then configure the selected state of a tab. This is necessary because in this app I don’t want the default white highlight that represents the selected tab. Pay attention to the image’s width, it must be the same of a single tab. In my case 320/4, 80 points wide.

    这个是选中按钮背景的状态

    1
    
    [[[self tabBarController] tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"selected"]];
    

    Last but not least, the UITabBarItem

    Unlike the default behavior the image shouldn’t change when the tab is selected, this is why I set the same image in both states. For each UIViewController that will be part of the tab bar you need to configure the tab image like this:

    在这设置选中图片和未选中状态的图片. 30*30px  @2x-> 60*60px

    1
    2
    3
    4
    5
    6
    7
    
    - (id)init {
        self = [super initWithNibName:@"MyNibName" bundle:nil];
        if (self) {
            self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"The Title" image:nil tag:0];
            [[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:@"tab_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_icon"]];
        }
    }
    

    The last detail is the title’s color on the unselected tab, they can’t be the default gray color. To change the color we need a dictionary of attributes whit the UITextAttributeTextColor key:

    1
    2
    3
    4
    
    // below the setFinishedSelectedImage:withFinishedUnselectedImage:
    [[self tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
            [UIColor whiteColor], UITextAttributeTextColor,
            nil] forState:UIControlStateNormal];
    

    That’s all folks.

    References

     
  • 相关阅读:
    Javascript面向对象编程--原型字面量
    Javascript面向对象编程--原型(prototype)
    Javascript面向对象编程--封装
    java word操作
    uniapp获取mac地址,ip地址,验证设备是否合法
    element-ui+vue表单清空的问题
    mysql,oracle查询当天的数据
    vue+element在el-table-column中写v-if
    idea修改页面不用重启项目(转)
    vue+element实现表格v-if判断(转)
  • 原文地址:https://www.cnblogs.com/LoveJiaQi/p/3144697.html
Copyright © 2011-2022 走看看