ios 导航栏出现在带有新 iOS7 SDK 的视图上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18103715/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:38:21  来源:igfitidea点击:

Navigation bar appear over the views with new iOS7 SDK

iphoneiosobjective-cipadios7

提问by One Man Crew

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 statusbarand navigation bar. The tableviewis appearing below the search bar

在早期版本中,它可以正常工作。搜索栏出现在statusbar导航栏下方。该tableview是出现在搜索栏下方

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.

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

Will it be fixed with iOS 7stable release ?

它会通过iOS 7稳定版本修复吗?

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?

或者我们应该通过添加 y(y = statubar height + nav bar height)值来处理它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.

我最近下载了 Xcode 5 DP 来在 iOS 7 中测试我的应用程序。我注意到并确认的第一件事是我的视图边界并不总是调整大小以考虑状态栏和导航栏。

In viewDidLayoutSubviews, I print the view's bounds:

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

{{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:

我已经找到了解决这个特定问题的方法。将导航栏的半透明属性设置为 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.

但是,当您希望导航栏半透明时,我还没有找到解决方法。例如,在全屏查看照片时,我希望导航栏是半透明的,并且视图在其下方被框起来。这有效,但是当我切换显示/隐藏导航栏时,我遇到了更奇怪的结果。第一个子视图 (a UIScrollView) 每次都更改其边界 y 原点。

回答by One Man Crew

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

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

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

You need add the above in your -(void)viewDidLoadmethod.

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

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

注意:您现在应该使用最新的 GM 版本的 iOS 7 和 Xcode 5,因为 API 已从测试版更改。

回答by MQoder

Screenshot from storyboard

故事板截图

If you are working in Storyboard (which I strongly recommend!) this is the solution:You can disable "Extend Edges" of your ViewController in storyboard. You have to do this for each viewController. You can disable extended edges by clicking the viewController icon in stortyboard (besides the productOwner beneath the view itself) and then selecting the attributes inspector (Like the images shows).

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

This will also set the alignment lines like iOS 6.

这也会像 iOS 6 一样设置对齐线。

Another great tool in xCode 5 is "Preview": click on the butler Button (assistant editor) and select Preview. there you can select iOS 6 and see how your storyboard design will look like on iOS 6.

xCode 5 中另一个很棒的工具是“预览”:单击管家按钮(助理编辑器)并选择预览。在那里,您可以选择 iOS 6 并查看您的故事板设计在 iOS 6 上的外观。

Its just great:D

太棒了:D

[Update]

[更新]

Be careful: disabling "Extend Edges" might result in a black glow on the navigation bar when the app enters background on iOS7. the glow will also be visible on the multitasking view (double press on home button). this can be solved by setting the background color of the navigation's bar view to white.

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

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

回答by Bids

As the OP says, there is a simple solution to this which is to set the navigation bar to be opaque. Rather than doing this in code, simply untick "Translucent" for your root navigation bar:

正如 OP 所说,对此有一个简单的解决方案,即将导航栏设置为不透明。无需在代码中执行此操作,只需取消根导航栏的“半透明”:

enter image description here

在此处输入图片说明

回答by yhlin

self.edgesForExtendedLayout=UIRectEdgeNone;

self.edgesForExtendedLayout=UIRectEdgeNone;

It works on iOS 7 simulator(Xcode 5 DP5)

它适用于 iOS 7 模拟器(Xcode 5 DP5)

回答by jasonpurdy

these answers were all helpful, especially MQoder's, but for me i also had to set the default top bar to "opaque black navigation".

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

enter image description here

在此处输入图片说明

回答by Yossi

@One Man Crew's answer is correct, but:

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

I would recommend to use this code to avoid errors when running the app on older versions :

我建议使用此代码以避免在旧版本上运行应用程序时出错:

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

回答by Enrico Susatyo

If you want to keep the transparency when the user scrolls your table view, you can set the contentInset of it:

如果要在用户滚动 table view 时保持透明度,可以设置它的 contentInset:

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

回答by EIMEI

self.edgesForExtendedLayout = UIRectEdgeNone;

self.edgesForExtendedLayout = UIRectEdgeNone;

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

您需要在 AppDelegate#application:didFinishLaunchingWithOptions 上执行此操作:

self.window.backgroundColor = [UIColor whiteColor];

self.window.backgroundColor = [UIColor whiteColor];

Otherwise Navigation Bar's background color will changed to gray. Because the transparent Navigation Bar overlaps window.

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

回答by Jayprakash Dubey

One solution is to use Navigation Controller. This automatically solve the issue. Also use Xcode 5 instead of Xcode Preview versions since they are beta ones.

一种解决方案是使用导航控制器。这会自动解决问题。也使用 Xcode 5 而不是 Xcode Preview 版本,因为它们是测试版。