将iOS TabBar项标题调整为其中心

时间:2013-02-11 11:57:58

标签: ios

//TabBarController code:

self.delegate=self;
self.tabBarController.tabBar.delegate=self;

CGRect viewFrame=self.tabBar.frame;
viewFrame.origin.y -=0;![enter image description here][1]
viewFrame.origin.x -=0;
viewFrame.size.height=30;
self.tabBar.frame=viewFrame;

firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL];
secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];

NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:
                                       self.firstViewController, self.secondViewController, nil];

self.viewControllers=twoViewControllers;



//    ====================================================
//    
//    FirstViewController code in initWithNibName: 
//    
//    To set the title of the first tabbar item:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Article view";
        NSLog(@"count = %d",[self.tabBarController.tabBar.items count]);

    }
    return self;
}

//如何在不向tabbaritem添加任何//图像的情况下将第一个标签栏项目标题“文章视图”发送到中心?

//类似于以下tabbar项目截图。

  [1]: http://i.stack.imgur.com/xBpVH.png


Thanks in advance.

1 个答案:

答案 0 :(得分:14)

替换initWithNibName方法

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Article view";
        self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
        NSLog(@"count = %d",[self.tabBarController.tabBar.items count]);
    }
    return self;
}

self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);此行此处以下列方式调整tabBarItem的图像位置:

  

将图像沿x方向'+5'移动,并沿y方向移动'-5'   默认位置。

UIEdgeInsetsMake一起玩,玩得开心。欢呼声。