简体   繁体   English

iPhone 6的自定义导航栏图像

[英]Custom navigation bar image with iphone 6

I'm trying to adapt my application for iphone 6. 我正在尝试使我的应用程序适合iPhone 6。

Everything was working well before when settings a custom background image to my navigation bar: 在将自定义背景图片设置到我的导航栏中之前,一切工作正常:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbarbg.png"] forBarMetrics:UIBarMetricsDefault];

Since this image is made for iphone 4/5, the width is not adequate. 由于此图像是为iPhone 4/5制作的,因此宽度不足。

Do you know how I could set a correct image for iphone6? 您知道如何为iphone6设置正确的图像吗?

I've tried to name my image navbarbg@3x.png or navabarbg-667h@3x.png but it doesn't change anything. 我尝试将图像命名为navbarbg@3x.png或navabarbg-667h@3x.png,但它没有任何改变。

Any idea ? 任何想法 ?

Update : I have added the image I use: 更新:我添加了我使用的图像:

在此处输入图片说明

May this also help, its work fine (if your image shadow or different )you can use this one also but different image you have to set as per scale of device: 也许这也有帮助,它的工作正常(如果您的图像阴影不同),您也可以使用此图像,但必须根据设备的比例设置不同的图像:

NSLog(@"width is :%f",[[UIScreen mainScreen] bounds].size.width);

UIImage *navBarImage =nil;
if ([[UIScreen mainScreen] bounds].size.width==375.0f) {
    navBarImage = [[UIImage imageNamed: @"header-topbg-iphone6@2x"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}
else if ([[UIScreen mainScreen] bounds].size.width==414.0f) {
    navBarImage = [[UIImage imageNamed: @"header-topbg-iphone6plus@3x"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}
else{
   navBarImage = [[UIImage imageNamed: @"header-topbg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

}

[[UINavigationBar appearance] setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];

you can see below image for iphone6+: 您可以在下面的图片中看到iphone6 +: 在此处输入图片说明 Thanks. 谢谢。

You should create a resizable image and set IT as the backgroundImage . 您应该创建一个可调整大小的图像,并将其设置为backgroundImage Based on the provided image, your left cap inset is 82.0f points. 根据提供的图像,您的左盖帽插图为82.0f点。 You can adjust this as needed to accommodate for the text. 您可以根据需要进行调整以适应文本。

Also, don't forget that @3x images are only used on the iPhone 6 Plus, NOT the iPhone 6. 另外,不要忘了, @3x的图像应用在iPhone 6此外, 不是 iPhone 6只使用。

UIImage *backgroundImage     = [UIImage imageNamed:@"your-image-name"];
UIImage *resizableBackground = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 82.0f, 0.0f, 0.0f)];
[self.navigationController.navigationBar setBackgroundImage:resizableBackground forBarMetrics:UIBarMetricsDefault];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM