视图隐藏在 UINavigationBar iOS 7 下方
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18824994/
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
View got hidden below UINavigationBar iOS 7
提问by Salman Zaidi
Earlier, I was using iOS 6.1 for my project. Recently I have switched to iOS 7. For, a lot of changes I knew, I updated my code.. But I have observed a strange behavior. My view on every screen gets hidden below navigation bar. Repositioning view solves the problem for iOS7, but creates problems for older iOS versions.
早些时候,我在我的项目中使用 iOS 6.1。最近我切换到了 iOS 7。对于我知道的很多变化,我更新了我的代码......但我观察到了一个奇怪的行为。我在每个屏幕上的视图都隐藏在导航栏下方。重新定位视图解决了 iOS7 的问题,但为旧的 iOS 版本带来了问题。
Can anyone explain me, what is the reason and why does it happen?? What has been changed in iOS 7 that's causing this problem??
谁能解释一下,这是什么原因,为什么会发生?导致此问题的 iOS 7 发生了什么变化?
Any help would be appreciated..
任何帮助,将不胜感激..
回答by Tarek Hallak
Try navigationBar.translucent = NO;
, It is YES
by default in iOS7.
试试吧navigationBar.translucent = NO;
,YES
iOS7默认是这样。
It is also good to take a look on this part of UINavigationBar
documentation:
看看这部分UINavigationBar
文档也很好:
New behavior on iOS 7. Default is YES. You may force an opaque background by setting the property to NO. If the navigation bar has a custom background image, the default is inferred from the alpha values of the image—YES if it has any pixel with alpha < 1.0 If you send setTranslucent:YES to a bar with an opaque custom background image it will apply a system opacity less than 1.0 to the image. If you send setTranslucent:NO to a bar with a translucent custom background image it will provide an opaque background for the image using the bar's barTintColor if defined, or black for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
iOS 7 上的新行为。默认为 YES。您可以通过将该属性设置为 NO 来强制使用不透明背景。如果导航栏有自定义背景图像,默认值是从图像的 alpha 值推断出来的 - 如果它有任何 alpha < 1.0 的像素,则为 YES 如果您将 setTranslucent:YES 发送到具有不透明自定义背景图像的栏,它将应用图像的系统不透明度小于 1.0。如果您将 setTranslucent:NO 发送到具有半透明自定义背景图像的栏,它将使用栏的 barTintColor(如果已定义)为图像提供不透明背景,如果 barTintColor 为零,则为 UIBarStyleBlack 提供黑色或为 UIBarStyleDefault 提供白色。
Edit:
编辑:
Setting 'navigationBar.translucent' value causes exception if you run project in devices/simulators having older iOS versions.
如果您在具有较旧 iOS 版本的设备/模拟器中运行项目,则设置 'navigationBar.translucent' 值会导致异常。
So you can add a version check like this:
所以你可以像这样添加一个版本检查:
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0)
{
navigationBar.translucent = NO;
}
Another option would be to set:
另一种选择是设置:
vc.edgesForExtendedLayout = UIRectEdgeNone;
Swift 3:
斯威夫特 3:
vc.edgesForExtendedLayout = []
回答by Daniel Broad
You can stop your views going under the navigation bar, in your viewController:
您可以在 viewController 中停止导航栏下的视图:
self.edgesForExtendedLayout = UIRectEdgeNone;
回答by David Seek
Swift 3+:
斯威夫特 3+:
self.edgesForExtendedLayout = []
回答by gladimdim
If you do not need translucent navigation bar in your app you can fix this on iOS7 and iOS6 without code changes.
如果您的应用中不需要半透明导航栏,您可以在 iOS7 和 iOS6 上修复此问题,而无需更改代码。
In storyboard select your navigation controller and then open "Attributes Inspector". Then under "Simulated Metrics" set "Top Bar" to some value but not to "translucent":
在故事板中选择您的导航控制器,然后打开“属性检查器”。然后在“模拟指标”下将“顶栏”设置为某个值但不要设置为“半透明”:
Now your views on iOS6 and iOS7 will have the same positioning as before.
现在你对 iOS6 和 iOS7 的看法将和以前一样定位。
回答by Lawrence Kesteloot
Point #7 on this listdoes the trick. You still have to wrap it in iOS 7-checking code like @null's answer:
此列表中的第 7 点可以解决问题。您仍然必须将其包装在 iOS 7 检查代码中,例如@null 的回答:
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0) {
viewController.edgesForExtendedLayout = UIRectEdgeNone;
}
The whole article is useful to those transitioning to iOS 7.
整篇文章对那些过渡到 iOS 7 的人很有用。
回答by Aks
Use this property for your VC, in-order to avoid overlap of ur statusbar with your VC Swift :
将此属性用于您的 VC,以避免您的状态栏与您的 VC Swift 重叠:
self.edgesForExtendedLayout = []
Objective C
目标 C
self.edgesForExtendedLayout = UIRectEdgeNone;
回答by Guy Kogus
Look up this key: UIViewControllerBasedStatusBarAppearance
.
查找这个键:UIViewControllerBasedStatusBarAppearance
.
It's used in your app's info PLIST file and will come up as:
它用于您应用的信息 PLIST 文件中,并将显示为:
View controller-based status bar appearance
查看基于控制器的状态栏外观
This will allow you to control the status bar's appearance. There's a bunch of API changes for status bars, go have a look in the documentation for new UIViewController
methods such as
这将允许您控制状态栏的外观。状态栏有很多 API 更改,请查看文档以了解新UIViewController
方法,例如
- (void)prefersStatusBarHidden;
- (void)prefersStatusBarHidden;
回答by matthew
In Swift 2.2, use
在 Swift 2.2 中,使用
self.edgesForExtendedLayout = .None
回答by saltwat5r
For me the best way for transparent Navigation Bar
is to change the shadowImage and backgroundImage of the bar.
对我来说,透明的最好方法Navigation Bar
是更改栏的 shadowImage 和 backgroundImage。
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.backgroundColor = nil
navigationController?.navigationBar.setBackgroundImage(UIImage(named: "navBarBackground")?.resizableImage(withCapInsets: .zero, resizingMode: .stretch), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()