本文介绍了有没有办法在不使用 UINavigationController 的情况下更改 Storyboard 中 UINavigationBar 的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一个视图中使用自定义 UINavigationBar,它不是任何 UINavigationController 层次结构的一部分.当我将 UINavigationBar 拖到 Storyboard 中时,它显示如下:

这个栏是 44px,如果状态栏不共享这个空间就足够了.因为 iOS7 允许我们使用整个屏幕,包括状态栏的空间,所以 UINavigationBar 应该是 64px 高,而不是 44px.

如果我将视图连接到 UINavigationController 的层次结构,那么它显示正确:

我在某处读到 if UINavigationBar 的属性 barPosition: 设置为 UIBarPositionTopAttached,然后是条形将是 64 像素.然而,这是一个 readonly 属性.

我的搜索结果只显示了我认为解决方法"的内容.通过在这个UIViewController之前添加一个无用的UINavigationController,我可以假装有层次,它会自动添加64px的UINavigationBar.p>

真的没有办法拥有一个覆盖 64 像素的流氓"(没有导航控制器的帮助)UINavigationBar?如果是这样,是否有任何正当理由说明原因?

(我不是说 UINavigationBar 默认应该是 64px,但在检查器中应该有一个选项)

(我也看到人们用编程方式来解决这个问题.即使这些答案有效,我仍然必须设计考虑到这一点的情节提要(差距).我想要什么要知道是否可以在情节提要中设置它,或者更确切地说,为什么不允许这样做?)

解决方案

你可以将属性 barPosition 设置为 UIBarPositionTopAttached 其他方式!

  1. 为您的 UINavigationBar 添加一个代理.
  2. 在委托类中实现-positionForBar::

    - (UIBarPosition)positionForBar:(id)bar{返回 UIBarPositionTopAttached;}

您的导航栏顶部也必须锚定到顶部布局指南.

I want to use a custom UINavigationBar in one of my views, which is not a part of any UINavigationController-hierarchy. When I drag a UINavigationBar into Storyboard, it shows like this:

This bar is 44px, which would be enough if the status bar wouldn't share this space. Because iOS7 lets us use the entire screen, including the space for the status bar, the UINavigationBar should be 64px tall, not 44px.

If I connect the view to a UINavigationController's hierarchy, then it shows correct:

I read somewhere that if the UINavigationBar has the property barPosition: set to UIBarPositionTopAttached, then the bar would be 64px. This is, however, a readonly-property.

My search results have only shown something I consider a "work-around". By adding a useless UINavigationController before this UIViewController, I can pretend to have a hierarchy, and it will add the UINavigationBar with 64px automatically.

Is there really no way to have a 'rogue'(without the help of a navigation controller) UINavigationBar that covers 64px?If that is the case, is there any valid reasons as to why?

(I'm not saying the UINavigationBar should be 64px by default, but there should be an option for it in the inspector)

(I also see people answering with programmatic ways to solve this. Even though these answers works, I'd still have to design the storyboard with that in mind (a gap). What I want to know is if it's possible to set this in storyboard, or rather, why isn't it allowed?)

解决方案

You can set the property barPosition to UIBarPositionTopAttached another way!

  1. Add a delegate to your UINavigationBar.
  2. Implement -positionForBar: in the delegate class:

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

Your navigation bar's top must also be anchored to the Top Layout Guide.

这篇关于有没有办法在不使用 UINavigationController 的情况下更改 Storyboard 中 UINavigationBar 的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:28