如何防止 UINavigationBar 在 iOS 7 中覆盖视图顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18953509/
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
How to prevent UINavigationBar from covering top of view in iOS 7?
提问by Sam D20
After updating to Xcode 5, the navigation bars in all of my app's views have shifted down. Here are some screenshots, the first showing everything in the view as it's pulled down, and the second showing all of it untouched. The search bar should begin where the navigation bar.
更新到 Xcode 5 后,我所有应用程序视图中的导航栏都向下移动。这是一些屏幕截图,第一个显示视图中的所有内容被拉下,第二个显示所有内容原封不动。搜索栏应该从导航栏开始。
Anyone know how I can fix this?
有谁知道我该如何解决这个问题?
edit: i have tried this previously recommendation:
编辑:我以前尝试过这个建议:
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
But it yields very odd results.
但它产生了非常奇怪的结果。
This may be because I have a "slide menu" under this view controller that is appearing due to the transparency of the navigation bar.
这可能是因为我在这个视图控制器下有一个“幻灯片菜单”,这是由于导航栏的透明度而出现的。
回答by Deepesh
Set the navigation bar's translucent property to NO:
将导航栏的半透明属性设置为 NO:
self.navigationController.navigationBar.translucent = NO;
This will fix the view from being framed underneath the navigation bar and status bar.
这将修复视图被框在导航栏和状态栏下方。
If you have to show and hide the navigation bar, then use
如果必须显示和隐藏导航栏,请使用
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone; // iOS 7 specific
in your viewDidLoad
method.
在你的viewDidLoad
方法中。
回答by Sumit Mundra
In iOS 7
by defaults all Controller translucent property value is YES, so you set translucent property NOfor this issue.
在iOS 7
通过默认所有控制器半透明的属性值是YES,那么你没有设置透光性这个问题。
self.navController.navigationBar.translucent = NO;
回答by Achyut Sagar
回答by uplearnedu.com
This works for swift as well on iOS 8.1
这也适用于 iOS 8.1
navigationController?.navigationBar.translucent = false
回答by jbouaziz
If you want to keep the translucency on your navigationBar
, at the end of your viewDidLoad
or in your viewWillAppear
add this line of code:
如果你想保持半透明,在你navigationBar
的末尾viewDidLoad
或在你的viewWillAppear
添加这行代码:
[self.view sendSubviewToBack:self.tableView]
Somehow if your scrollView
subclass (UITableView
, UICollectionView
, etc.) is at index
0 in your current view subviews
, it will automatically adjust the insets according to your navigationBar
. And it shouldn't affect your UI in versions prior to iOS7 either.
不知何故,如果您的scrollView
子类(UITableView
、UICollectionView
等)index
在您当前的视图中为0 subviews
,它会根据您的navigationBar
. 它也不应该影响 iOS7 之前版本中的 UI。
EDITIf you initialize your UITableView
programmatically, then it is best to add it to the view using this [self.view insertSubview:self.tableView atIndex:0];
编辑如果您以UITableView
编程方式初始化,那么最好使用此方法将其添加到视图中[self.view insertSubview:self.tableView atIndex:0];
回答by Md. Najmul Hasan
Swift 4:
斯威夫特 4:
Set following line of code in viewDidLoad method:
在 viewDidLoad 方法中设置以下代码行:
self.navigationController?.navigationBar.isTranslucent = false
回答by malkoty
回答by Krivoblotsky
Another approach is to set self.automaticallyAdjustsScrollViewInsets = YES;
on your view controller.
This is enabled by default. But in your case:
另一种方法是self.automaticallyAdjustsScrollViewInsets = YES;
在您的视图控制器上设置。这是默认启用的。但在你的情况下:
I see you are using EGORefreshHeaderView. It plays with contentInset of UITableView. So when you release it, header will reset top inset instead of restore previous value.
我看到您正在使用 EGORefreshHeaderView。它使用 UITableView 的 contentInset。所以当你释放它时,header 将重置顶部插入而不是恢复以前的值。
回答by Mr Jox
The above answers might fix your issue but rise many other issues, for instance: Custom views are not centered by Y because navigation bar becomes opaque hence it pushes down all your views.
上面的答案可能会解决您的问题,但会引发许多其他问题,例如:自定义视图不以 Y 为中心,因为导航栏变得不透明,因此它会下推您的所有视图。
Having translucent navigation bar doesn't cause this issue neither it causes the issue you posted about in iOS 11+. However I found no workaround to have it work on all iOS platforms starting from iOS 9.
具有半透明导航栏不会导致此问题,也不会导致您在 iOS 11+ 中发布的问题。但是我发现没有解决方法可以让它在从 iOS 9 开始的所有 iOS 平台上工作。
回答by Farhad Malekpour
If you want to have complete control on views and avoid faulty adjustments of iOS, subclass UITableView and adjust the insets (both scroll and indicators) in -(void)willMoveToWindow:(UIWindow *)newWindow. Works for me.
如果您想完全控制视图并避免 iOS 的错误调整,请继承 UITableView 并调整 -(void)willMoveToWindow:(UIWindow *)newWindow 中的插图(滚动和指示器)。为我工作。