如何防止 UINavigationBar 覆盖 iOS7中的顶部视图?

在更新到 Xcode 5之后,我所有应用程序视图中的导航栏都向下移动了。这里有一些截图,第一个显示了视图中的所有内容,当它被拉下来时,第二个显示了所有未被触及的内容。搜索栏应该从导航栏的位置开始。

All Content All Content on Idle

有人知道我该怎么补救吗?

编辑: 我已经尝试了以前的建议:

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;

但它产生了非常奇怪的结果。

Solution Attempt

这可能是因为我在这个视图控制器下面有一个“幻灯片菜单”,这是由于导航栏的透明性而出现的。

99350 次浏览

In iOS 7 by defaults all Controller translucent property value is YES, so you set translucent property NO for this issue.

self.navController.navigationBar.translucent = NO;

Set the navigation bar's translucent property to 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.

Another option is to open your Info.plist file in source code mode and enter the following info:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarHidden</key>
<true/>

Hope this helps.

You can add this method into your view controller as shown on this URL:

-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.searchBar.frame =
CGRectMake(0, self.topOfViewOffset, self.searchBar.frame.size.width, self.searchBar.frame.size.height);
}

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:

[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.


EDIT If you initialize your UITableView programmatically, then it is best to add it to the view using this [self.view insertSubview:self.tableView atIndex:0];

Another approach is to set self.automaticallyAdjustsScrollViewInsets = YES; on your view controller. This is enabled by default. But in your case:

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.

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.

This works for swift as well on iOS 8.1

navigationController?.navigationBar.translucent = false

You can disable the "Extend edges" in Attribute inspector of View Controller of this screen (as shown in below image) :

enter image description here

Swift 4:

Set following line of code in viewDidLoad method:

self.navigationController?.navigationBar.isTranslucent = false