[英]How to change the background image of tab bar in Objective-C?
I am develop in objective-C. 我正在用Objective-C开发。 I want to change the tab bar background like the following picture: 我想更改标签栏背景,如下图:
And the code is like the following: 并且代码如下所示:
UIImage *tabBarBackground = [UIImage imageNamed:@"tabbaritem_background.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
But after setting the background image , the background is not at the correct place like the following: 但是设置背景图像后,背景不在正确的位置,如下所示:
The background image should be place at the bottom like the background in above picture. 背景图片应像上图的背景一样放置在底部。
Did I missing something ? 我错过了什么吗? Can someone help me ? 有人能帮我吗 ?
Thanks in advance. 提前致谢。
I think is somewhere you go wrong, check if is this steps: 我认为您出了问题的地方,请检查以下步骤:
In the ViewController.m
you can set the tabbar bacground color: 在ViewController.m
您可以设置标签栏免费颜色:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [[UITabBar appearance] setBackgroundColor:[UIColor grayColor]]; // Here you can set the converted color form image, make sure the imageSize fit. }
The result is below: 结果如下:
我认为可以尝试的方法- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
,因为您的backgroudimage的大小不等于tabbar'size。
Try this method to change your image to a ScaleImage. 尝试使用此方法将您的图像更改为ScaleImage。
+(UIImage *)getScaleImageNamed:(NSString *)name{
UIImage *nomalImage = [UIImage imageNamed:name];
CGFloat hInset = floorf(nomalImage.size.width / 2);
CGFloat vInset = floorf(nomalImage.size.height / 2);
UIImage *res = [nomalImage resizableImageWithCapInsets:UIEdgeInsetsMake(vInset, hInset, vInset, hInset)];
return res;
}
Steps you may miss, hope that can help. 您可能会错过的步骤,希望对您有所帮助。
Assets.xcassets
) 确保已导入背景图像(例如,在Assets.xcassets
) resizableImageWithCapInsets:
to resize the background image 使用resizableImageWithCapInsets:
调整背景图片的大小 Put the UIAppearance
settings in AppDelegate.m
: 将UIAppearance
设置放入AppDelegate.m
:
[[UITabBar appearance] setBackgroundImage:[[UIImage imageNamed:@"tabbaritem_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.