将iOS TabBar项目标题调整为其中心 [英] Adjust iOS TabBar item title to the centre of it

查看:109
本文介绍了将iOS TabBar项目标题调整为其中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//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?

//How can i make the first tabbar item title "Article View" to the Center without adding any //image to the tabbaritem ?

//类似于下面的tabbar项目截图。

// similar to the below tabbar items screenshot.

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


Thanks in advance.


推荐答案

替换 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的图像位置:

self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0); this line here adjusts the position of the image for the tabBarItem in the manner :



默认位置向x方向'+5'和y方向'-5'移动图像。

Shift the image in x-direction '+5' and in y-direction '-5' from the default position.

UIEdgeInsetsMake 并玩得开心。干杯。

这篇关于将iOS TabBar项目标题调整为其中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆