ios 选择项目时如何更改 UITabItem 的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17879066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change background color of UITabItem when item is selected
提问by user1898829
I would like a different background color when the user selects a tab bar item than when it is unselected.
当用户选择标签栏项目时,我想要一个不同的背景颜色,而不是未选择时。
回答by user1898829
Put this in the Appdelegate.m
in application didFinishLaunchingWithOptions
把这个Appdelegate.m
放在application didFinishLaunchingWithOptions
UIImage *whiteBackground = [UIImage imageNamed:@"whiteBackground"];
[[UITabBar appearance] setSelectionIndicatorImage:whiteBackground];
回答by serdaryillar
if you use a storyboard or xibs, click "Tab Bar" and add "selectedImageTintColor" path into the Key Path Attributes tag. Like this :
如果您使用故事板或 xib,请单击“标签栏”并将“selectedImageTintColor”路径添加到关键路径属性标签中。像这样 :
回答by abinop
In Swift
在斯威夫特
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabSelected")
with an image [email protected]
of size 98x98 pixels
图像[email protected]
大小为 98x98 像素
回答by Andy
UPDATE: As of iOS 7.1 this technique no longer works (if the user taps the same tab twice in succession, the background colour is cleared).
更新:从 iOS 7.1 开始,此技术不再有效(如果用户连续两次点击同一个选项卡,背景颜色将被清除)。
UITabBarItem
is a subclass of UIBarItem
, everything is more painful because UIBarItem doesn't subclass UIView
; however, UITabBarItem
containsone. What follows manipulates that view, and therefore might be rejected if submitted to the AppStore.
UITabBarItem
是 的子类UIBarItem
,一切都更痛苦,因为 UIBarItem 没有子类UIView
;然而,UITabBarItem
包含一个。下面的操作会操纵该视图,因此如果提交到 AppStore 可能会被拒绝。
1) Subclass UITabBarItem
1) 子类 UITabBarItem
Create a subclass of UITabBarItem and add a new selected
property to its header, like so:
创建一个 UITabBarItem 的子类,并selected
在其头部添加一个新属性,如下所示:
@interface ALDTabBarItem : UITabBarItem
@property (nonatomic, assign, getter = isSelected) BOOL selected;
@end
UITabBarItems have a view property, but it isn't exposed. We can extend the class to access it, and then create a custom setter on the selected
property to change the background colour, like so:
UITabBarItems 有一个 view 属性,但它没有公开。我们可以扩展类来访问它,然后在selected
属性上创建一个自定义的 setter来更改背景颜色,如下所示:
#import "ALDTabBarItem.h"
@interface ALDTabBarItem (ALD)
@property (nonatomic, strong) UIView *view;
@end
@implementation ALDTabBarItem
- (void)setSelected:(BOOL)selected
{
if(selected)
self.view.backgroundColor = [UIColor redColor];
else
self.view.backgroundColor = [UIColor clearColor];
}
@end
2) Update your UITabBarController delegate
2) 更新您的 UITabBarController 委托
Add the following code to the delegate of your UITabBarController, which sets the selected states of the UITabBar:
将以下代码添加到您的 UITabBarController 的委托中,它设置 UITabBar 的选定状态:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
for(ALDTabBarItem *myItem in tabBar.items)
myItem.selected = (myItem == item);
}
回答by Mehul Thakkar
Follow this Steps:
请按照以下步骤操作:
Create SubClass of
UITabBarController
Go to
viewDidAppear
ofUITabBarController
subclassNow Find the size of TabBarItem,
UITabBar *tabBar = self.tabBar; CGSize imgSize = CGSizeMake(tabBar.frame.size.width/tabBar.items.count,tabBar.frame.size.height);
Now Create the image with that size,
//Create Image UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0); UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,imgSize.width,imgSize.height)]; [[UIColor blueColor] setFill]; [p fill]; UIImage* finalImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
Now, Assign this image to TabBar's
SelectionIndicatorImage
[tabBar setSelectionIndicatorImage:finalImg];
创建子类
UITabBarController
去
viewDidAppear
的UITabBarController
子类现在找到 TabBarItem 的大小,
UITabBar *tabBar = self.tabBar; CGSize imgSize = CGSizeMake(tabBar.frame.size.width/tabBar.items.count,tabBar.frame.size.height);
现在创建具有该大小的图像,
//Create Image UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0); UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,imgSize.width,imgSize.height)]; [[UIColor blueColor] setFill]; [p fill]; UIImage* finalImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
现在,将此图像分配给 TabBar 的
SelectionIndicatorImage
[tabBar setSelectionIndicatorImage:finalImg];
Swift 4 Version:
斯威夫特 4 版本:
let imgSize = CGSize(width: tabBar.frame.size.width / CGFloat(tabBar.items!.count),
height: tabBar.frame.size.height)
UIGraphicsBeginImageContextWithOptions(imgSize, false, 0)
let p = UIBezierPath(rect: CGRect(x: 0, y: 0, width: imgSize.width,
height: imgSize.height))
UIColor.blue.setFill()
p.fill()
let finalImg = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UITabBar.appearance().selectionIndicatorImage = finalImg
回答by stan
My answer is similar to @Mehul Thakkarbut it is in Swift 4and to improve on his answer I would say that if you place the code in viewDidAppear
as he suggests users will see the change happening which is not good user experience.
我的答案与@Mehul Thakkar类似,但它在Swift 4 中,为了改进他的答案,我想说的是,如果您viewDidAppear
按照他的建议放置代码,用户会看到发生的变化,这不是很好的用户体验。
So create the custom class for your tabbar controller and in viewDidLoad
place the following code:
因此,为您的标签栏控制器创建自定义类并viewDidLoad
放置以下代码:
let singleTabWidth: CGFloat = self.tabBar.frame.size.width / CGFloat((self.tabBar.items?.count)!)
let singleTabSize = CGSize(width:singleTabWidth , height: self.tabBar.frame.size.height)
let selectedTabBackgroundImage: UIImage = self.imageWithColor(color: .white, size: singleTabSize)
self.tabBar.selectionIndicatorImage = selectedTabBackgroundImage
The imageWithColor
function is below for you:
该imageWithColor
功能是低于你:
//image with color and size
func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor)
context!.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
Hope this helps someone.
希望这可以帮助某人。
回答by Ayaz
Please refer below URL's.
请参考以下网址。
Changing Tint / Background color of UITabBar
How To Change Tab bar color in Xcode
hope this will help you..
希望能帮到你..
try this to change tabbar item color but it only work in ios5.
试试这个来改变标签栏项目的颜色,但它只适用于 ios5。
if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)])
{
[tabBarController.tabBar setSelectedImageTintColor:[UIColor redColor]];
}
回答by Kishore Kumar
Answer in swift 4:
在 swift 4 中回答:
setSelectedImageTintColor is deprecated on iOS 8.
setSelectedImageTintColor 在 iOS 8 上已弃用。
Instead use this :
而是使用这个:
self.tabBar.tintColor = UIColor.white
self.tabBar.tintColor = UIColor.white
回答by OhadM
Currently in Xcode 8.3.2 you can do it inside the storyboard using an image that will represent the actual background.
目前在 Xcode 8.3.2 中,您可以使用代表实际背景的图像在情节提要中执行此操作。
Select the tab bar inside your tab bar controller:
选择标签栏控制器内的标签栏:
Inside the Utilities choose the Attributes Inspector and change the selection background image:
在 Utilities 中选择 Attributes Inspector 并更改选择背景图像:
回答by Boris Nikolic
Put this in your AppDelegate.m file:
把它放在你的 AppDelegate.m 文件中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[UITabBar appearance].selectionIndicatorImage = [UIImage imageNamed:@"activeTabBackgroundImage"];
return YES;
}