使用新的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];

在早期版本中,它运行正常。搜索栏显示在状态栏和导航栏下方。 tableview 出现在搜索栏下方

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

但是当我在 Xcode 5上运行时sdk iOS 7 ,搜索栏不可见(我认为它位于状态栏和导航栏下方),导航栏也出现在表格视图上。

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.

是否可以用 iOS 7 稳定版本修复?

Will it be fixed with iOS 7 stable release ?

或者它是我编码的问题?

Or is it the problem of my coding ?

或者我们应该通过添加y 来处理它(y = statubar height +导航栏高度) iOS 7的值

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

我最近下载了Xcode 5 DP来测试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.

在viewDidLayoutSubviews中,我打印视图的边界:

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?

更新:

我找到了针对这个特定问题的解决方案。将导航栏的半透明属性设置为NO:

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.

但是,当您希望导航栏为半透明时,我还没有找到针对此案例的修复程序。例如,全屏查看照片,我希望导航栏是半透明的,并且要在其下面构建视图。这是有效的,但当我切换显示/隐藏导航栏时,我经历了甚至更奇怪的结果。第一个子视图( UIScrollView )每次都会更改其边界y原点。

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.

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

That’s not entirely true. There has been a new property introduced in iOS 7 that lets you adjust the layout behavior as in previous versions of iOS. Place this piece of code in your view controller, and you should be good to go! The space your navigation bar takes up should be accounted for automatically

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

您需要在 - (void)viewDidLoad $中添加以上内容c $ c>方法。

注意:由于API已从beta版本发生变化,因此您应该使用iOS 7和Xcode 5的最新GM版本。

Note: You should be using the latest GM release of iOS 7 and Xcode 5 now since the API has changed from beta versions.