的iOS UITabBarItem图像selectedImage和图像显示不正确
的Xcode 8.2.1的iOS UITabBarItem图像selectedImage和图像显示不正确
- (instancetype)init {
if (self = [super init]) {
self.title = self.localStringModel.tab_title_market;
self.tabBarItem.image = [UIImage imageName:@"unselected"];
self.tabBarItem.selectedImage =[UIImage imageName:@"selected"];
}
return self;
}
描述: 当我未被选择的tabBarItem的unselectedIamge车灯不能显示。
图片:
它已经困扰我很久了,我尝试使用UIImageRenderingModeAlwaysOriginal
但不行,顺便说一下我的图像资源的“PDF”,谁能帮助我?
目标C
@interface BaseClassVC(){
UITabBarController *tabBarController;
}
@end
@implementation BaseClassVC
- (void)viewDidLoad {
[super viewDidLoad];
[self setTabBarItemAction];
}
-(void)setTabBarItemAction{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]];
// UITabBarController *tabBarController = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:@"Hometab"];
tabBarController = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:@"Hometab"];
// Assign tab bar item with titles
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 ;///= [tabBar.items objectAtIndex:4];
tabBarItem1.title = @"Home";
tabBarItem2.title = @"My cards";
tabBarItem3.title = @"Withdraw Locations";
tabBarItem4.title = @"Taxi";
tabBarItem5.title = @"More..";
tabBarItem3.badgeValue = @"0";
tabBarItem4.badgeValue = @"0";
[tabBarItem1 setImage:[[UIImage imageNamed:@"icn_tab_home_Normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:@"icn_tab_home_Selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setImage:[[UIImage imageNamed:@"icn_nearby_deals_tab"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:@"icn_nearby_deals_tab_Selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setImage:[[UIImage imageNamed:@"icn_tab_ATM_Normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:@"icn_tab_ATM_Selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setImage:[[UIImage imageNamed:@"icn_tab_Taxi_Normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setSelectedImage:[[UIImage imageNamed:@"icn_tab_Taxi_Selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem5 setImage:[[UIImage imageNamed:@"icn_tab_more_Normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem5 setSelectedImage:[[UIImage imageNamed:@"icn_tab_more_Normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
tabBarController.tabBar.translucent = false;
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: TABBAR_NORMAL_TEXT_COLOURS, NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
TABBAR_SELECTED_TEXT_COLOURS, NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];
[[UITabBar appearance] setBarTintColor:ThemeColor];
UIImage *whiteBackground = [Utility imageWithColor:TABBAR_SELECTED_COLOURS];
[[UITabBar appearance] setSelectionIndicatorImage:whiteBackground];
tabBarController.tabBar.translucent = false;
/*
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateHighlighted];
*/
[self.navigationController showViewController:tabBarController sender:nil];
// [self presentViewController:tabBarController animated:YES completion:nil];
}
雨燕2.3
override func viewDidLoad() {
super.viewDidLoad()
methodTabBarController()
}
extension BaseClassVC {
func methodTabBarController() -> Void{
let storyboard = UIStoryboard(name: "HomePage", bundle: NSBundle.mainBundle())
let tabBarController:UITabBarController = (storyboard.instantiateViewControllerWithIdentifier("idTabBarHometab") as? UITabBarController)!
methodCustomtabBar(tabBarController)
// presentViewController(tabBarController, animated: true, completion: nil)
self.navigationController!.showViewController(tabBarController, sender: nil)
// self.navigationController?.pushViewController(tabBarController, animated: true)
}
func methodCustomtabBar(tabBarController: UITabBarController) -> Void{
// Assign tab bar item with titles
let tabBar: UITabBar = tabBarController.tabBar
let tabBarItem1: UITabBarItem = tabBar.items![0] as UITabBarItem
let tabBarItem2: UITabBarItem = tabBar.items![1] as UITabBarItem
let tabBarItem3: UITabBarItem = tabBar.items![2] as UITabBarItem
let tabBarItem4: UITabBarItem = tabBar.items![3] as UITabBarItem
tabBarItem1.title = "Home"
tabBarItem2.title = "Nearby ATM"
tabBarItem3.title = "Near By Shops"
tabBarItem4.title = "Taxi"
tabBarItem2.badgeValue = "0"
tabBarItem3.badgeValue = "0"
tabBarItem4.badgeValue = "0"
tabBarItem1.image = UIImage(named: "icn_tab_home_Normal")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem1.selectedImage = UIImage(named: "icn_tab_home_Selected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem2.image = UIImage(named: "icn_tab_withdrawl_location_Normal")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem2.selectedImage = UIImage(named: "icn_tab_withdrawl_location_Selected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem3.image = UIImage(named: "icn_tab_nearbyshop_Normal")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem3.selectedImage = UIImage(named: "icn_tab_nearbyshop_Selected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem4.image = UIImage(named: "icn_tab_Taxi_Normal")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabBarItem4.selectedImage = UIImage(named: "icn_tab_Taxi_Selected")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
// Sets the default color of the icon of the selected UITabBarItem and Title
// UITabBar.appearance().tintColor = UIColor.redColor()
// Sets the default color of the background of the UITabBar
UITabBar.appearance().barTintColor = ThemeColor
// Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar)
// UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))
/*
UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))
// Uses the original colors for your images, so they aren't not rendered as grey automatically.
for item in self.tabBar.items as! [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithRenderingMode(.AlwaysOriginal)
}
}
}
*/
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: ColorConstant.TABBARITEMS.setTitleTextNormal], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: ColorConstant.TABBARITEMS.setTitleTextSelected], forState:.Selected)
}
}
谢谢你。“imageWithRenderingMode”它的工作原理。 –
欢迎家伙.. –
我认为问题在于您的未选定图像在灯光中具有纯白色。尝试使用透明度填充它们,alpha = 0.0。它始终是用于绘制的图像的Alpha通道。
请看看这个。 Change tab bar item selected color in a storyboard
它会解决你的问题。
对UITabBar使用tintColor和unSelectedItemTintColor属性。
ü可以检查我的回答和回应我 –
谢谢i将imageRenderingMode到UIImageRenderingModeAlwaysOriginal它的工作原理。我犯了一个错误,我设置了tabBarI在childController中创建图像。 –