问题
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