UITabBarItem Change Image Height and Width

柔情痞子 提交于 2019-12-12 02:33:38

问题


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.

I tried:

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);

but nothing changed.

Thank you!


回答1:


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

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);
}


来源:https://stackoverflow.com/questions/20499356/uitabbaritem-change-image-height-and-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!