How to clip larger images to fit into tabBar icons in tabBarController built programmatically.

风格不统一 提交于 2019-12-05 05:01:18
Sample_image is larger in size than is required by tab.

Try this piece of code as this will resize the required image and return an UIImage instance with 30x30 size (size required for UITabbar).

UIImage *image = [UIImage imageNamed:@"Sample_Image.png"];
self.tabBarItem.image = [self imageWithImage:image scaledToSize:CGSizeMake(30, 30)];

Add This method

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Rename your image to Sample_Image@2x.png. This is called pixel doubling for the Retina Display.

Without the @2x iOS doesn't know that it should apply a scale factor and it will be used as it is and though it should be halved.

 [[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"Sample_Image@2x.png"]];

In reality there should be:

Sample_Image png (45 px or so)

Sample_Image@2x.png so you say only:

[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"Sample_Image.png"]];

If your icons are not fitting in the tabbar then copy the same image into 2x image asset or 3x image asset instead of 1x asset. It will fit properly and it's worked for me

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