设置tabar

来源:互联网 发布:最好的笔记软件 编辑:程序博客网 时间:2024/06/14 10:55

做一个app,首先需要把所有与之相关的图片导入Assets.xcassets,因为app需要图标,而且点击进入后有一个缓冲画面。

标签条 tabbar  (在此之前要创建一个窗口: self.window = [ [ UIWindow alloc ]  init ] ;

                                                                       self.window.frame = [ UIScreen mainSrceen ] .bounds ;)让窗口的屏幕大小等于模拟器屏幕的尺寸

        1. 创建一个UITabBar 对象 :  

                                   UITabBar *  tabBar = [ [  UITabBar alloc] init ];

        2. 创建若干个 UITabBarItem 对象   : 

                                   UITabBarItem* tabItem1 =  [ [ UITabBarItem alloc ] init ];

        3. 创建 UITabBarController ,作为程序窗口的根控制器:

                                  ( self.window.rootViewController =  [ [UITabBarController alloc ] init ] ;  

                                     重新创建一个类,把要创建的子控制器及其参数全部放进该类。)

                                   UITabBarController  * tabBarController =[ [ UITabBarController alloc]  init]

        4.创建多个UIViewController子类的实例,即添加子控制器:

                     UIViewController  *vc = [ [ UIViewController alloc ] init ]

                     a.  编辑子控制器的名称:

                                  vc . tabBarItem . title = @"name"

                     b.  编辑子控制器的图片:

                                   未选中时:vc.tabBarItem .image = [UIImage imageNamed : @"name"]

                                   选中时:vc.tabBarItem . selectedImage = [ UIImage imageNamed : @"name"]

                     c.  编辑子控制器的背景颜色:

                                  vc . view . backgroundColor = [UIColor   redcolor]


           [ tabBarController  addChildViewController : vc];添加为子控制器


选中子控制器时,子控制器的图片会被子控制器的背景颜色自动渲染,改进方法:

              第一种: UIImage *image  =  [ imageNamed : @"xxxxx"] ; 

                             image = [image imageWithRenderingMode : UIImageRenderingModeAlwaysOriginal ];

                             vc.tabBarItem.selecdtedImage = image;

              第二种:在assets xcassets,找到图片后在右边设置。

    设置子控制器的字体:  

  此时为未选中时

         NSMutableDictionary *attrs =  [ NSMutableDictionary dictionary] ;

       字体大小:

          attrs [NSFontAttributeName = [UIFont systemFontOfSize : 13];

       字体颜色:

          attres [NSForegroundColorAttributeName] = [ UIColor  darkColor ];

           [ vc. tabBarItem setTitleTextAttributes: attrs forState: UIControlStateNormal] ;


     选中时:

         NSMutableDictionary *selectedAttrs=  [ NSMutableDictionary dictionary] ;

       字体大小:

         selectedAttrs [NSFontAttributeName = [UIFont systemFontOfSize : 13];

       字体颜色:

         selectedAttrs [NSForegroundColorAttributeName] = [ UIColor  darkColor ];

           [ vc. tabBarItem setTitleTextAttributes: selectedAttrs forState: UIControlStateSeletedl] ;

整个方法名的后缀有UI_APPEARENCE_SELETOR  时,就可不用重新设置其他要求相同的子控制器,(通过appearence统一设置 TabBarItem的文字属性)

该用下面方法设置:

       UITabBarItem *item = [ UITabBarItem appearance];

      [item setTitleTextAttributes: attrs forState:UIControlStateNormal];

      [ item settitleTextAttributes:selectedattrs forState: UIControlStateSelected];

为减少每次创建子控制器打一大堆代码,改用  设置一个新方法   来初始化子控制器:

     -(void)setupChildvc: (NSString *)title image:(NSString *) image seletedImagg:(NSString *)selectedImge

{

UIViewController *vc =[ UIViewController alloc] init];

vc.tabBarItem.title = title;

vc.tabBarItem.image = [ UIImage imageNamed:image];

vc.tabBarItem.selectedImage = [ UIImage imageNamed:selectedImage] ;

vc.view.backgroundColor = [UIColor 

[self addChildViewController:vc];

}

然后就可以创建一个子控制器,传入相应的参数,即可设置成功一个子控制器。

[self setupChildvc:@"name"        image:@"name"          seletedImage:@"name"];

该方法有个弊端,每次调用都要生成一个UIViewController ,而实际上,需要根据情况判断是否需要生成其他类别的控制器。为了改进,将初始化一个控制器放在方法声明中,不放在方法里。改进后代码:

     -(void)setupChildvc:   ( UIViewController *)vc  title:(NSString *)title image:(NSString *) image seletedImage:(NSString *)selectedImge

{

vc.tabBarItem.title = title;

vc.tabBarItem.image = [ UIImage imageNamed:image];

vc.tabBarItem.selectedImage = [ UIImage imageNamed:selectedImage] ;

vc.view.backgroundColor = [UIColor 

[self addChildViewController:vc];

}

[self setupChildvc:[ [ UIViewController alloc] init ]  title:@"name"        image:@"name"          seletedImage:@"name"];

为所有的子控制器分别建立一个类,以后在开发子控制器控制的内容就方便许多。

自定义tabbar:

  单独创建一个类来完成tabbar的自定义( 以buton为例):

先发布该按钮:

           @property (nonatomic , weak) UIButton *pButton;

初始化该按钮:

          -(instancetype)initWithFrame:(CGRect)frame

{

     if(self = [super initWithFrame:frame]) 

          {

               UIButton *pButton = [UIButton buttonWithType:UIButton:UIButtonTypeCustom];

                [ pButton setBackgroundIamge : [UIImage imageNamed:@"name"] forState:UIControlStateNormal];

                [pButton setBackgroundImage:[UIImage imageNamed:@"name"] forState:UIControlStateHighlighted];

                [ self addSubview:pButton];

                self.pButton = pButton;}

               return self;

  设置按钮的frame(将其放在中间位置,将其他已经设置好的tabbar为它腾出一定的位置空间):

 设置其他UITabBarButton的frame

-(void)layoutSubviews

{

  [super layoutSunviews];

self.pButton.bounds = CGRectMake(0,0,self.pButton.currentBackgroundImage.size.width, self.pButton.currentBackgroundImage.size.height);

self.pButton.center = CGPointMake(self.frame.size.width * 0.5,self.frame.size.height * 0.5);


 CGFloat button1 = 0;

CGFloat button2 = self.frame.size.width/5;

CGFloat button3 = self.frame.size.height;

NSInteger index = 0;

for(UIView *button in self.subviews){

if( ![button isKindOfClass:[UIControl class] ] || button == self.pButton)     continue;

 计算按钮的x值:

 CGFloat button1 = button2 *((index >1)?(index + 1): index);

button.frame = CGRectMake(button1,button2,button3,button4);

index++;


}

如果不能修改属性,用 KVC  来间接操作对象属性:

     [ self. tabBar =[ [ xxxTabBar alloc] init];

    [ self setValue: [ [ xxxTabBar alloc] init] forKeyPath:@"tabBar"];

0 0
原创粉丝点击