Navigation bar appear over the views with new iOS7 SDK

CGRect cgRect1 = [[UIScreen mainScreen] applicationFrame];




UISearchBar  *mySearchBar = [[UISearchBar alloc]
initWithFrame:CGRectMake(0, 0, cgRect.size.width, 40)];


mySearchBar.autoresizingMask =
UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight ;




UITableView  *myTableView = [[UITableView alloc]
initWithFrame:CGRectMake(0, 40, cgRect.size.width, cgRect.size.height-40)];


myTableView.autoresizingMask =
UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;




[self.view addSubview:mySearchBar];
[self.view addSubview:myTableView];

In the earlier versions it is working correctly. The search bar is appearing below the statusbar and navigation bar. The tableview is appearing below the search bar

But when I run this on Xcode 5 sdk iOS 7, the search bar is not visible (I think its placed under the status bar and navigation bar) , and also the navigation bar is appearing over the table view.

Will it be fixed with iOS 7 stable release ?

Or is it the problem of my coding ?

Or should we handle it by adding the y (y = statubar height + nav bar height) value for iOS 7 ?

I recently downloaded Xcode 5 DP to test my apps in iOS 7. The first thing I noticed and confirmed is that my view's bounds is not always resized to account for the status bar and navigation bar.

In viewDidLayoutSubviews, I print the view's bounds:

{{0, 0}, {320, 568}}

This results in my content appearing below the navigation bar and status bar.

I know I could account for the height myself by getting the main screen's height, subtracting the status bar's height and navigation bar's height, but that seems like unnecessary extra work.

Has anyone else experienced this issue?

UPDATE:

I've found a solution for this specific problem. 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.

However, I have not found a fix for the case when you want the navigation bar to be translucent. For instance, viewing a photo full screen, I wish to have the navigation bar translucent, and the view to be framed underneath it. That works, but when I toggle showing/hiding the navigation bar, I've experienced even stranger results. The first subview (a UIScrollView) gets its bounds y origin changed every time.

73757 次浏览

不完全是这样。在 iOS7中引入了一个新属性,可以像以前版本的 iOS 一样调整布局行为。将这段代码放到视图控制器中,应该就可以了!导航栏占用的空间应该自动计算

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

您需要在 -(void)viewDidLoad方法中添加上述内容。

注意: 您现在应该使用通用公司最新发布的 iOS7和 Xcode 5,因为 API 已经从 beta 版本发生了变化。

self.edgesForExtendedLayout=UIRectEdgeNone;

It works on iOS 7 simulator(Xcode 5 DP5)

Screenshot from storyboard

如果你在 Storyboard 工作(我强烈推荐!) ,这是一个解决方案: 您可以在故事板中禁用 ViewController 的“扩展边缘”。您必须为每个 viewController 执行此操作。您可以通过单击故事板中的 viewController 图标(除了视图本身下面的 productOwner 之外) ,然后选择属性检查器(如图所示)来禁用扩展边缘。

这也将设置对齐线,如 iOS6。

XCode 5中另一个很棒的工具是“ Preview”: 单击 butler Button (助理编辑器)并选择 Preview。在那里你可以选择 iOS6,看看你的故事板设计在 iOS6上会是什么样子。

真是太棒了 D

[更新]

小心: 当应用程序进入 iOS7的背景时,禁用“扩展边缘”可能会导致导航栏上出现黑光。在多任务视图中也可以看到发光(双击主页按钮)。这可以通过将导航栏视图的背景颜色设置为白色来解决。

[self.navigationController.view setBackgroundColor:[UIColor whiteColor]];

Self.edgesForExtendedLayout = UIRectEdgeNone;

And you need to do this on AppDelegate#application:didFinishLaunchingWithOptions:

self.window.backgroundColor = [UIColor whiteColor];

否则导航栏的背景颜色将改为灰色。因为透明导航栏与窗口重叠。

这些答案都很有帮助,尤其是 MQder 的,但对我来说,我还必须设置默认的顶部栏为“不透明的黑色导航”。

enter image description here

一种解决方案是使用导航控制器,这会自动解决问题。 也使用 Xcode 5代替 Xcode 预览版,因为它们是 beta 版。

如果希望在用户滚动表视图时保持透明,可以设置其 contentInset:

CGFloat topLayoutGuide = self.topLayoutGuide.length + self.tabBarController.navigationController.navigationBar.frame.size.height;
self.tableView.contentInset = UIEdgeInsetsMake(topLayoutGuide, 0, 0, 0);

正如 OP 所说,有一个简单的解决方案,就是将导航栏设置为不透明的。与其在代码中这样做,不如在你的根导航栏中取消“半透明”选项:

enter image description here

@ One Man Crew 的回答是正确的,但是:

我建议使用这段代码来避免在旧版本上运行应用程序时出现错误:

 #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
#endif