更改 iOS 中 uiTabBarItem 的可调整大小背景 image/color
Change resizable background image/color of uiTabBarItem in iOS
我需要完成一个相当简单的任务。
我有一个标签栏,需要在选中时更改栏的背景颜色。
我一直在寻找解决方案的尴尬时间。
大多数指南都在谈论 TabBarItem 的 SelectedImage 属性,但我需要这个东西的大小完全动态。一张图片在一台设备上只能适应一个方向。
这是我要完成的任务的屏幕截图:
理想情况下,我只想将所选选项卡的背景颜色设置为白色。有人可以告诉我这是怎么做到的吗?
非常感谢!
这可能无法完全解决您的问题,但可能会给您提示 UIImage
的大小可以是动态的
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
发布这个以防以后有人需要它。
这个解决方案由三部分组成。
第 1 部分。
将 UITabBar 'Item Positioning' 属性 从自动设置为居中。这将允许您设置标签栏项目的宽度。标签栏项目的高度将始终为 49。
第 2 部分。
使用此函数根据颜色和特定大小创建 UIImage:
- (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContext(size);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
// Draw your image
[image drawInRect:rect];
// Get the image, here setting the UIImageView image
image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
第 3 部分。
现在您已准备好实际设置选项卡项目的背景颜色。
[[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor whiteColor] forSize:CGSizeMake(tabBar.itemWidth, 49) withCornerRadius:0]];
我不明白为什么这不是在 Interface Builder 中实现的。但这有效。
我需要完成一个相当简单的任务。
我有一个标签栏,需要在选中时更改栏的背景颜色。
我一直在寻找解决方案的尴尬时间。
大多数指南都在谈论 TabBarItem 的 SelectedImage 属性,但我需要这个东西的大小完全动态。一张图片在一台设备上只能适应一个方向。
这是我要完成的任务的屏幕截图:
理想情况下,我只想将所选选项卡的背景颜色设置为白色。有人可以告诉我这是怎么做到的吗?
非常感谢!
这可能无法完全解决您的问题,但可能会给您提示 UIImage
的大小可以是动态的
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
发布这个以防以后有人需要它。
这个解决方案由三部分组成。
第 1 部分。 将 UITabBar 'Item Positioning' 属性 从自动设置为居中。这将允许您设置标签栏项目的宽度。标签栏项目的高度将始终为 49。
第 2 部分。 使用此函数根据颜色和特定大小创建 UIImage:
- (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContext(size);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
// Draw your image
[image drawInRect:rect];
// Get the image, here setting the UIImageView image
image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
第 3 部分。 现在您已准备好实际设置选项卡项目的背景颜色。
[[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor whiteColor] forSize:CGSizeMake(tabBar.itemWidth, 49) withCornerRadius:0]];
我不明白为什么这不是在 Interface Builder 中实现的。但这有效。