问题描述
我使用Image作为导航栏背景图片。要设置Image,我使用以下代码:
I am using Image as Navigation bar background Image. To set Image I used following code:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_logo_ios7.png"] forBarMetrics:UIBarMetricsDefault];
对于iOS7nav_logo_ios7.png图像大小为768x64,对于iOS6和以下我用过的图像有大小768x44。
For iOS7 "nav_logo_ios7.png" image size is 768x64 and for iOS6 and bellow I used image has size 768x44.
这适用于所有 UIViewControllers
。
在同一个项目中,我使用 UIActivityViewController
。在iOS7上,邮件撰写视图如下所示:
In same project I am using UIActivityViewController
. On iOS7 mail compose view look like this:
我如何处理这个?
提前致谢。
推荐答案
您面临的问题是当UIViewController以模态方式呈现时,状态栏不包含在UINavigationBar的高度中。
The issue you are facing is that when a UIViewController is presented modally, the status bar is not included in the height of the UINavigationBar.
这意味着64pt图像不正确。
This means that the 64pt image is incorrect.
首先,官方更好检查设备运行的iOS版本的方法是执行以下操作:
First of all, the official and better way to check what version of iOS the device is running would be to do something like this:
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
//handle iOS 7 Stuff
}
else
{
//handle older iOS versions
}
有关更多信息,请查看 NSObjCRuntime.h
标题。
For more information, check out the NSObjCRuntime.h
header.
UINavigationBar背景图片不应该是一个固定大小的图像,而应该是可伸缩的图像,如可重复的图案,所以也许重新考虑未来的设计是一个想法...但是,如果你想继续使用自定义固定大小的图像,那么我有给你一个建议......
UINavigationBar background images shouldn't really be a fixed size image and instead should be stretchable image such as a repeatable pattern so maybe it would be an idea to rethink future designs... However if you do want to continue with a custom fixed sized image then I have a suggestion for you...
UINavigationController允许你使用 ...这意味着您可以使用不同的UINavigationBar子类以模态方式呈现任何视图,而不是模态显示的视图。
The UINavigationController allows you to initialise an instance with custom UINavigationBar and UIToolbar classes using initWithNavigationBarClass:toolbarClass:
... This means that you could init any views that you are not presenting modally with a different UINavigationBar subclass to views that are being modally presented.
这意味着您可以根据导航控制指定不同的背景图像ller是否以模态呈现,例如:
This means that you will be able to specify different background images dependant on if your navigation controller is modally presented or not, for example:
UIImage *backgroundImage44pts = [UIImage imageNamed:@" ... "];
UIImage *backgroundImage64pts = [UIImage imageNamed:@" ... "];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
//handle iOS 7 Stuff
[[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault];
[[UINavigationBarSubclass appearance] setBackgroundImage:backgroundImage64pts forBarMetrics:UIBarMetricsDefault];
}
else
{
//handle older iOS versions
[[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault];
}
需要注意的一件重要事情是MFMailComposeViewController不是真正的视图控制器,所以尝试使用自定义导航栏子类初始化它可能无法正常工作..这就是为什么我为所有非模态导航控制器使用自定义导航栏子类而不是相反的方式。
One important thing to note is that the MFMailComposeViewController isn't a real view controller so trying to initialise it with custom navigation bar subclasses may not work.. That is why I have used a custom navigation bar subclass for all non-modal navigation controllers and not the other way round.
另外需要注意的是,如果你的应用程序是通用的,那么模态视图就不存在了(除非你有任何自定义的东西),你不必担心这个。
Another thing to note would be that if your application is universal then modal views do not exist (unless you have anything custom) and you would not have to worry about this.
正如我之前所说的那样...... UINavigationBars并不是真的设计为具有固定大小的背景图像(这就是为什么它很难实现)所以如果你认为这个工作是太复杂了,也许重新考虑你的设计是个好主意。
As I said earlier... UINavigationBars aren't really designed to have fixed sized background images (this is why it is so difficult to achieve) so if you think this work around is too complicated then maybe it would be a good idea to rethink your design.
最后一件事(我保证)... iOS 7中的一个主要设计变化是从状态栏下方的导航栏中获取您的内容。添加图像以防止这种情况并用纯白色背景替换它对于iOS 7应用来说似乎很奇怪。
And one last thing (I promise)... One of the main design changes in iOS 7 is to have your content from the navigation bar flowing underneath the status bar.. Adding an image to prevent this and replace it with a solid white background seems rather strange for an iOS 7 app.
这篇关于iOS 7 Navigationbar背景图片问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!