iOS 7 状态栏与 NavigationBar 冲突

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

iOS 7 Status Bar Collides With NavigationBar

iosobjective-cuinavigationcontrolleruinavigationbarios7

提问by AJ112

I have a view controller in my app that has a navigation bar dragged on it in the storyboard. It was working fine in the iOS 6 but in iOS 7 it look like this:

我的应用程序中有一个视图控制器,它在故事板中拖动了一个导航栏。它在 iOS 6 中运行良好,但在 iOS 7 中它看起来像这样:

ios 7 status bar nav var

ios 7 状态栏导航变量

The status bar and the navigation bar should no collide with each other. I have seen a lot of such questions on the stack overflow but they didn't of much help to me.

状态栏和导航栏不应相互碰撞。我在堆栈溢出上看到了很多这样的问题,但它们对我没有多大帮助。

Some questions say that i should use this "self.edgesForExtendedLayout = UIRectEdgeNone;" but it didn't work. Some say i should remove the navigation bar and embed it inside the navigation controller that i cannot do due to the way my program is implemented. Some solutions suggests to use the view bounds and all but it didn't work for me as well.

有些问题说我应该使用这个“self.edgesForExtendedLayout = UIRectEdgeNone;” 但它没有用。有人说我应该删除导航栏并将其嵌入到导航控制器中,但由于我的程序的实现方式,我无法做到这一点。一些解决方案建议使用视图边界,但它对我也不起作用。

What is the one thing that can help me resolve this issue. Thanks in advance!

可以帮助我解决此问题的一件事是什么。提前致谢!

UPDATE:I have embedded the view controller inside a uinavigation controller. Removed the navigation bar that was earlier manually added in it. Now it looks ok in the storyboard but when i run it, it shows the following:

更新:我已将视图控制器嵌入到 uinavigation 控制器中。删除了之前手动添加的导航栏。现在它在故事板中看起来没问题,但是当我运行它时,它显示以下内容:

iOS 7 Navigation Status bar

iOS 7 导航状态栏

It is showing text from another view controller that is currently behind it that is its parent view controller. Means its transparent now. Can anyone point out what i am doing wrong?

它显示来自当前位于其父视图控制器后面的另一个视图控制器的文本。意味着它现在是透明的。谁能指出我做错了什么?

回答by KC.

The latest version of the iOS has brought many visual changes and from a developer's point of view, the navigation and status bar are two noticeable changes.

最新版本的 iOS 带来了很多视觉上的变化,从开发者的角度来看,导航和状态栏是两个明显的变化。

The status bar is now transparent and navigation bar behind it shows through. The navigation bar image can even be extended behind the status bar.

状态栏现在是透明的,它后面的导航栏显示出来。导航栏图像甚至可以扩展到状态栏后面。

First of all, if you are a beginner and have just started iOS development and are confused the way status bar and navigation bar is working, you can simply go through a blog post HEREthat i found very useful. It has all the information related to navigation and status bar in iOS 7.

首先,如果你是一个初学者,刚开始iOS开发和感到困惑的方式状态栏和导航栏的工作,你可以简单地经过一个博客帖子这里,我发现非常有用的。它包含与 iOS 7 中导航和状态栏相关的所有信息。

Now coming to the answer of your question. First of all i can see two different problems. One is that your status bar and navigation bar are both kind of colliding with each other as shown by you in the question with an image.

现在来回答你的问题。首先,我可以看到两个不同的问题。一个是您的状态栏和导航栏都相互碰撞,如您在问题中用图像显示的那样。

PROBLEM:Well the problem is that your have earlier dragged a navigation bar in your view controller which was working in iOS 6 correctly but with the arrival of iOS 7 SDK, this approach is resulting in status bar and navigation bar overlapping with each other.

问题:问题是您之前在视图控制器中拖动了一个导航栏,该导航栏在 iOS 6 中正常工作,但随着 iOS 7 SDK 的到来,这种方法导致状态栏和导航栏相互重叠。

SOLUTION to First Problem:You can either use UIBarPositionTopAttached or you can use view bounds and frames, i can also suggest and link you to Apple's documentationand bla bla bla but that would take some time for you to solve the issue.

第一个问题的解决方案:您可以使用 UIBarPositionTopAttached 或者您可以使用视图边界和框架,我也可以建议您并将您链接到Apple 的文档和 bla bla bla ,但这需要一些时间来解决问题。

The best and the most easiest way to solve this issue is to just embed your view controller inside a navigation controller and thats it. You can do it by just selecting the view controller and going to Editor > Embed In > Navigation Controller. (If there is any content on your old navigation bar, you can first drag it down, embed the view controller in navigation controller and then move the bar buttons on the new navigation bar and then delete the old navigation bar)

解决这个问题的最好和最简单的方法是将你的视图控制器嵌入到导航控制器中,就是这样。您只需选择视图控制器并转到 Editor > Embed In > Navigation Controller 即可完成此操作。(如果旧导航栏有内容,可以先往下拉,将视图控制器嵌入到导航控制器中,然后移动新导航栏上的栏按钮,然后删除旧导航栏)

SOLUTION to Second Problem:This solution is for your specific question that you have mentioned in the update and is not for the general public reading this. As you can see that navigation and status bar is not visible and a transparent area is showing the parent view controller. I am not really use why you are facing this issue but most probably because of some third party library like ECSlidingView or any other is involved. You can select this view controller in your storyboard and set the background color of the view to be the same as your navigation bar. This will stop showing the parent view controller behind and your navigation bar and status bar will start showing. Now you can cover the rest of your view controller with text view or what ever your are using in it.

第二个问题的解决方案:此解决方案适用于您在更新中提到的特定问题,而不是供普通大众阅读。如您所见,导航和状态栏不可见,并且透明区域显示父视图控制器。我并没有真正了解您为什么面临这个问题,但很可能是因为涉及到一些第三方库,如 ECSlidingView 或任何其他库。您可以在故事板中选择此视图控制器并将视图的背景颜色设置为与导航栏相同。这将停止显示后面的父视图控制器,您的导航栏和状态栏将开始显示。现在,您可以使用文本视图或您在其中使用的内容覆盖视图控制器的其余部分。

Hope this helps!

希望这可以帮助!

回答by Scott Berrevoets

The navigation bar is too close to the status bar because starting in iOS 7, the status bar is more of an overlay over the whole view controller's view. Since your navigation bar is at (0, 0), the status bar will show on top of the navigation bar. To solve this, simply move the navigation bar down (or, as others have said), create a constraint between the navigation bar and the topLayoutGuide.

导航栏离状态栏太近,因为从 iOS 7 开始,状态栏更像是覆盖整个视图控制器的视图。由于您的导航栏位于 (0, 0),因此状态栏将显示在导航栏的顶部。要解决这个问题,只需向下移动导航栏(或者,正如其他人所说的那样),在导航栏和 topLayoutGuide 之间创建一个约束。

When you do that, you will see that there is now a 20 point gap between the navigation bar and the top of the screen. That's because you just moved the navigation bar down 20 points. "But UINavigationControllercan do it right!" Absolutely, and it does so by implementing UIBarPositioningDelegateon your view controller. This is a one-method protocol that should be implemented like this:

当你这样做时,你会看到导航栏和屏幕顶部之间现在有 20 点的差距。那是因为您刚刚将导航栏向下移动了 20 点。“但UINavigationController能做对!” 绝对可以,它是通过UIBarPositioningDelegate在您的视图控制器上实现来实现的。这是一个单方法协议,应该像这样实现:

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}

After adding your view controller as the delegate for the navigation bar, you'll notice the navigation bar is still shifted down 20 points, but its background will extend up underneath the status bar, just like in UINavigationController.

添加视图控制器作为导航栏的代理后,您会注意到导航栏仍向下移动 20 点,但其背景将向上延伸到状态栏下方,就像在UINavigationController.

Another thing you're seeing is that the navigation bar is translucent, meaning anything underneath the navigation bar will be visible to some extent. The translucentproperty on UINavigationBaris set to YESby default on iOS 7. Before iOS 7, the default was NO.

您看到的另一件事是导航栏是半透明的,这意味着导航栏下方的任何内容在某种程度上都是可见的。该translucent属性上UINavigationBar设置为YES默认在iOS 7的iOS 7前,默认为NO

回答by Masterfego

you can simply do this:

你可以简单地这样做:

1) add a constrain between the Navigation Barand Top Layout Guide(select navigationBar, hold ctrl key and go to Bottom Layout Guide, unhold ctrl key)

1) 在导航栏顶部布局指南之间添加约束(选择导航,按住 ctrl 键并转到底部布局指南,取消按住 ctrl 键)

Add vertical Contrain

添加垂直约束

2) select vertical spacing:

2)选择垂直间距

vertical spacing

垂直间距

3) set constant to 0:

3) 将常量设置为0

vertical spacing constant

垂直间距常数

Result:

结果:

result

结果

UPDATE

更新

In your AppDelegate file you can add this:

在您的 AppDelegate 文件中,您可以添加以下内容:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{
  // Prevent Navigationbar to cover the view
  UINavigationBar.appearance().translucent = false
}

回答by Jageen

This works for me i hope you also have same luck :).
Add below code in your view.

这对我有用,我希望你也有同样的运气:)。
在您的视图中添加以下代码。

-(void) viewDidLayoutSubviews
{
    CGRect tmpFram = self.navigationController.navigationBar.frame;
    tmpFram.origin.y += 20;
    self.navigationController.navigationBar.frame = tmpFram;
}

It basically change location of navigation bar.

它基本上改变了导航栏的位置。

回答by PJR

First, set UIViewControllerBasedStatusBarAppearanceto NO in Info.plist. Then, in AppDelegate's application:didFinishLaunchingWithOptions:method add:

首先,UIViewControllerBasedStatusBarAppearance在 Info.plist 中设置为 NO。然后,在 inAppDelegateapplication:didFinishLaunchingWithOptions:方法中添加:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
   [application setStatusBarStyle:UIStatusBarStyleLightContent];
   self.window.clipsToBounds = YES;
   self.window.frame = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height-20);
   self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
 }
 return YES;

回答by PgmFreek

This is new feature with IOS7. Instead of staring at 20 px navigation bar in IOS7 staring at 0 px. As a solution shift the whole view downwards to 20 px or you can use image for navigation bar with height 64px.

这是IOS7的新功能。而不是在 IOS7 中盯着 20 px 导航栏盯着 0 px。作为解决方案,将整个视图向下移动到 20 像素,或者您可以使用高度为 64 像素的导航栏图像。

回答by Derek Gogol

In case it still helps someone, this is what worked for me for moving the Navigation Bar little bit down in ios 7 and above:

如果它仍然对某人有帮助,这对我在 ios 7 及更高版本中将导航栏向下移动一点点有用:

-(void)viewWillLayoutSubviews
{
    float iosVersion = 7.0;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= iosVersion) {
        // iOS 7+
        CGRect viewFrame = self.view.frame;
        viewFrame.origin.y += 10;
        self.view.frame = viewFrame;
    }
}

On a device with ios 6.1 and below the Navigation Bar will be unchanged, as it was before.

在 ios 6.1 及以下的设备上,导航栏将保持不变,就像以前一样。

And this is what I used to make the contents of the Status Bar lighter:

这就是我用来使状态栏的内容更亮的方法:

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

回答by Nate Potter

If your UIViewControlleris NOT in a UINavigationControllerand you're using UIStoryBoard, you can set the "iOS 6/7 Deltas" to 20 for the delta Y, for every subview that needs to be offset from the UIStatusBar.

如果您不在UIViewControllera 中UINavigationController并且正在使用UIStoryBoard,则可以将增量 Y 的“iOS 6/7 Deltas”设置为 20,对于每个需要从UIStatusBar.

enter image description here

在此处输入图片说明

回答by Victor Sigler

Using Swift:

使用 Swift

As @Scott Berrevoets said in his answer you need to implement the method positionForBarin the protocol UIBarPositioningDelegate, but as the UINavigationBarDelegateprotocol implements this protocol :

正如@Scott Berrevoets 在他的回答中所说,您需要positionForBar在协议中实现该方法UIBarPositioningDelegate,但是由于UINavigationBarDelegate协议实现了这个协议:

public protocol UINavigationBarDelegate : UIBarPositioningDelegate {
   ...
}

You only need to set the delegateof the UINavigationBaryou set using Storyboard and implement the method and it's done, like in this way:

你只需要设置delegateUINavigationBar,你用故事板制定和执行的方法和它的完成,像这样:

class ViewController: UIViewController, UINavigationBarDelegate {

   @IBOutlet weak var navigationBar: UINavigationBar!

   override func viewDidLoad() {
       super.viewDidLoad()
       self.navigationBar.delegate = self 
   }

   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
   }

   func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
       return UIBarPosition.TopAttached
   }
}

NOTE:It's worth to mention if you set the position of the y-axisof the navigation bar, let's say to 40 from the top, then it will extend underneath to the top from this position, to simulate the behaviour of the UINavigationControlleryou need to set to 20 from the top.

注意:值得一提的是,如果你设置导航栏y轴的位置,假设从顶部40,那么它会从这个位置向下延伸到顶部,以模拟UINavigationController你需要的行为从顶部设置为 20。

I hope it will help you.

我希望它会帮助你。