UITabBar 图像在 iOS 7 上未正确显示
UITabBar images not showing properly on iOS 7
我的标签栏在 iOS 8 上正常工作,但在 iOS 7 上不能正常工作,有人可以指出我正确的方向吗,我使用 JSON 文件来配置我的 UITabBar当它加载第一个屏幕时。
我必须为 iOS 7 使用不同的图像吗??
可以更改 de Button 的大小来避免该行为吗??
iOS 8:
iOS 7:
这是我用来加载图片的代码:
- (void)createTabBar
{
tabController = [self.storyboard instantiateViewControllerWithIdentifier:@"cCustomTabController"];
dValue = [dConfiguration objectForKey:@"Buttons"];
NSMutableArray *aControllers = [[NSMutableArray alloc] init];
int i = 0;
for (NSString* sProperty in dValue) {
NSString* d = @"Details";
NetworkStatus internetStatus = [_reachabilityInfo currentReachabilityStatus];
NSData *itemData = Nil;
if (internetStatus != NotReachable)
itemData = [util getSpecificJsonData:[sProperty valueForKeyPath:@"Item"]];
if(itemData != nil){
UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
UIImage * iSelected = imageBtnPress;
iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tab setSelectedImage:iSelected];
tab.tag = i;
if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
[vcCustom setTabBarItem:tab];
[vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
[aControllers insertObject:navigationController atIndex:i];
i++;
}
else
{
UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
UIImage * iSelected = imageBtnPress;
iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tab setSelectedImage:iSelected];
tab.tag = i;
if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
[vcCustom setTabBarItem:tab];
[vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
UINavigationController *navigationController = [[cCustomNavigationController alloc] initWithRootViewController:vcCustom];
[aControllers insertObject:navigationController atIndex:i];
i++;
}
}
tabController.delegate = self;
tabController.viewControllers = aControllers;
tabController.tabBar.tintColor = [UIColor blackColor];
}
提前致谢。
确保使用包含@1x、@2x 和@3x 图像文件的资产目录以支持任何屏幕分辨率。
我个人使用 Mark Bridges 的 Asset Catalog Creator 来创建不同的分辨率。
编辑:
导入多分辨率资产目录后,您可以从界面生成器中使用 [UIImage imageNamed:@"myImage"];
或 select 它。 iOS会自动选择好的
我在代码中更改了这两行:
UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
对于这 2 个:
UIImage *imageBtn = [self changeImageSize:[UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]] scaledToSize:CGSizeMake(30, 30)];
UIImage *imageBtnPress = [self changeImageSize:[UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]] scaledToSize:CGSizeMake(30, 30)];
并添加了改变图片大小的功能:
- (UIImage *)changeImageSize:(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;
}
我不知道这是否正确,但对我来说很有效。
感谢所有快乐的编码。
确保使用包含@1x、@2x 和@3x 的资产目录。
例子:
yourimageName@2x~iphone.png for iphone
,
yourimageName@2x~ipad.png for ipad
我的标签栏在 iOS 8 上正常工作,但在 iOS 7 上不能正常工作,有人可以指出我正确的方向吗,我使用 JSON 文件来配置我的 UITabBar当它加载第一个屏幕时。
我必须为 iOS 7 使用不同的图像吗??
可以更改 de Button 的大小来避免该行为吗??
iOS 8:
iOS 7:
这是我用来加载图片的代码:
- (void)createTabBar
{
tabController = [self.storyboard instantiateViewControllerWithIdentifier:@"cCustomTabController"];
dValue = [dConfiguration objectForKey:@"Buttons"];
NSMutableArray *aControllers = [[NSMutableArray alloc] init];
int i = 0;
for (NSString* sProperty in dValue) {
NSString* d = @"Details";
NetworkStatus internetStatus = [_reachabilityInfo currentReachabilityStatus];
NSData *itemData = Nil;
if (internetStatus != NotReachable)
itemData = [util getSpecificJsonData:[sProperty valueForKeyPath:@"Item"]];
if(itemData != nil){
UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
UIImage * iSelected = imageBtnPress;
iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tab setSelectedImage:iSelected];
tab.tag = i;
if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
[vcCustom setTabBarItem:tab];
[vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
[aControllers insertObject:navigationController atIndex:i];
i++;
}
else
{
UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
UIImage * iSelected = imageBtnPress;
iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tab setSelectedImage:iSelected];
tab.tag = i;
if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
[vcCustom setTabBarItem:tab];
[vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
UINavigationController *navigationController = [[cCustomNavigationController alloc] initWithRootViewController:vcCustom];
[aControllers insertObject:navigationController atIndex:i];
i++;
}
}
tabController.delegate = self;
tabController.viewControllers = aControllers;
tabController.tabBar.tintColor = [UIColor blackColor];
}
提前致谢。
确保使用包含@1x、@2x 和@3x 图像文件的资产目录以支持任何屏幕分辨率。
我个人使用 Mark Bridges 的 Asset Catalog Creator 来创建不同的分辨率。
编辑:
导入多分辨率资产目录后,您可以从界面生成器中使用 [UIImage imageNamed:@"myImage"];
或 select 它。 iOS会自动选择好的
我在代码中更改了这两行:
UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
对于这 2 个:
UIImage *imageBtn = [self changeImageSize:[UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]] scaledToSize:CGSizeMake(30, 30)];
UIImage *imageBtnPress = [self changeImageSize:[UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]] scaledToSize:CGSizeMake(30, 30)];
并添加了改变图片大小的功能:
- (UIImage *)changeImageSize:(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;
}
我不知道这是否正确,但对我来说很有效。
感谢所有快乐的编码。
确保使用包含@1x、@2x 和@3x 的资产目录。
例子:
yourimageName@2x~iphone.png for iphone
,
yourimageName@2x~ipad.png for ipad