选项卡栏项目图像和 selectedImage
我有一个选项卡栏控制器(它是一个基于选项卡栏的应用程序,因此选项卡栏位于 MainWindow.xib 上)。在此 xib 中,我添加了 4 个选项卡栏项目,并设置了所有选项卡栏项目的图像。因此,我面临两个问题:
1)图像是白色的,但是当我运行应用程序时,它将选项卡栏项目上的所有图像显示为灰色。我怎样才能让它看起来和原始图像一样。
2)我有一个选定的图像,我想将其添加到当前选定的选项卡栏项目上。我该怎么做?
在 NICK 的代码之后更新:
嘿,在 iOS 5 中,您必须在应用程序委托中编写以下代码,用于设置选项卡栏项目选定和未选定的图像(类别解决方案仅适用于 4):
if ([[[UIDevice currentDevice] systemVersion] floatValue]>4.9) {
NSString *selectedImageName,*unselectedImageName;
for (int counter = 0; counter < [self.tabBarController.tabBar.items count]; counter++) {
if (counter==0) {
selectedImageName = <someImagename>;
unselectedImageName = <someImagename>;
}
else if (counter==1) {
selectedImageName = <someImagename>;
unselectedImageName = <someImagename>;
}
.
.
else {
selectedImageName = <someImagename>;
unselectedImageName = <someImagename>;
}
UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
UIImage *unselectedImage = [UIImage imageNamed:unselectedImageName];
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:counter];
if ([item respondsToSelector:@selector(setFinishedSelectedImage:withFinishedUnselectedImage:)]) {
[item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
}
}
}
I have a tab bar controller (its a tab bar based application, so tab bar is on MainWindow.xib). In this xib, I have added 4 tab bar items and I have set the image of all tab bar item. Due to this, I am facing 2 issues:
1) The image is white-colored, but when I run the application, its showing all the images on tab bar item as gray colored. How can I make it look same as is in the original image.
2) I have a selected image, that I want to add on the tab bar item which is currently selected. How should I do this???
UPDATED AFTER NICK's CODE:
Hey, in iOS 5, you will have to write following code in your app delegate for setting tab bar item selected and unselected image (the category solution will work only on 4):
if ([[[UIDevice currentDevice] systemVersion] floatValue]>4.9) {
NSString *selectedImageName,*unselectedImageName;
for (int counter = 0; counter < [self.tabBarController.tabBar.items count]; counter++) {
if (counter==0) {
selectedImageName = <someImagename>;
unselectedImageName = <someImagename>;
}
else if (counter==1) {
selectedImageName = <someImagename>;
unselectedImageName = <someImagename>;
}
.
.
else {
selectedImageName = <someImagename>;
unselectedImageName = <someImagename>;
}
UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
UIImage *unselectedImage = [UIImage imageNamed:unselectedImageName];
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:counter];
if ([item respondsToSelector:@selector(setFinishedSelectedImage:withFinishedUnselectedImage:)]) {
[item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此类别添加到您的项目中。它将强制选项卡栏项目使用您的原始图像作为禁用状态,而不是对其应用灰色渐变:
这可能看起来像是在使用私有 API,但我已经看到它在已批准的应用程序上多次使用。它实际上并不是调用私有方法,只是重写一个私有方法。
如果您需要为选定和未选定的图像指定不同的图像,最好的选择可能是使用 UITabBarItem 的 tag 属性和 switch 语句,如下所示:
然后在界面生成器中,不必费心设置选项卡栏项目图像,因为它们将被忽略。相反,将它们的标签设置为与您在 switch 语句中指定的图像匹配。
请注意,如果您的应用程序中有多个选项卡栏,并且您不希望以这种方式全部覆盖它们,则可以在 UITabBarItem 的子类上定义这些方法,而不是作为类别。然后,您可以将 nib 文件中的选项卡栏项目的类设置为自定义子类,而不是常规的 UITabBarItems,并且只有这些项目会受到影响。
编辑:
请注意,从 iOS 5 开始,有一种更好的方法可以使用 UIAppearance API 来执行此操作。这种技术应该仍然有效,但既然有了官方支持的方法,谁知道苹果是否会开始打击它。除非您确实需要 iOS 4 支持,否则最好使用新方法。
Add this category to your project. It will force tab bar items to use your original image as the disabled state instead of applying a grey gradient to them:
This may seem like it is using private APIs but I've seen this used multiple times on apps that were approved. It's not actually calling a private method, just overriding one.
If you need to specify different images for the selected and unselected image, your best bet is probably to use the tag property of the UITabBarItem and a switch statement, like this:
Then in interface builder, don't bother with setting the tab bar item images as they'll just be ignored. Instead, set their tags to match up with the images you've specified in your switch statements.
Note that if you have multiple tab bars in your app, and you don't want them to all be overridden in this way, you can define these methods on a subclass of UITabBarItem instead of as a category. Then you can set the class of the tab bar items in your nib file to be your custom subclass instead of regular UITabBarItems, and only those ones will be affected.
EDIT:
Note that as of iOS 5 there is a better way of doing this using the UIAppearance APIs. This technique should still work, but who knows if Apple might start cracking down on it now that there is an officially supported approach. Better to use the new method unless you really need iOS 4 support.
基于 http://blog.theanalogguy.be/ 对我有用。添加类别UItabBarItem(CustomUnselectedImage) - 还没有效果=(
*.h
和*.m
快乐编码=]-
Based on http://blog.theanalogguy.be/ works for me. Add the category UItabBarItem (CustomUnselectedImage) - haven't effect =(
the *.h
and *.m
happy coding =]-