UITabBarItem 改变图片高度和宽度 [英] UITabBarItem Change Image Height and Width

查看:45
本文介绍了UITabBarItem 改变图片高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有标题而只有图像的 UITabBarItem.我想知道如何更改图像大小以便它可以占用整个 UITabBarItem.

I have a UITabBarItem without a title and only an image. I was wondering how I can change the image size so it can take up the whole UITabBarItem.

我搜索了整个互联网,但一无所获.

I searched all over the internet but found nothing.

我试过了:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_Storyboard" bundle:nil];
UITabBarController *tabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"tab"];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);

但没有任何改变.

谢谢!

推荐答案

来自 Apple 文档,TabBar 上图片的最大尺寸为 30x30(视网膜显示为 60x60).

from apple documentation the maximum size of a picture on the TabBar are 30x30 (60x60 for the retina display).

我认为在不拉伸图像的情况下不可能占据 TabBar 的整个高度.我认为最好的解决方案是使用 imageInset 将图像在 TabBar 中居中

I do not think it's possible to take the entire height of the TabBar without strecth the image. I think the best solution is to center the image in the TabBar using imageInset

tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

否则你可以使用这个图像插入并像截图一样拉伸图像

otherwise you can play with this imageInset and strecth the image like in the screenshot

tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {


 UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

 UITabBar *tabBar = tabBarController.tabBar;
 UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
 UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
 UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

 //add image to tabbarItems

 tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
}

这篇关于UITabBarItem 改变图片高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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