简体   繁体   English

UINavigationController中的导航栏太短

[英]Navigation bar in UINavigationController too short

I'm putting together an app where some of the views are in regular UIViewControllers and use their own UINavigationBar, whereas others will be part of a navigation hierarchy inside a UINavigationController and make use of its UINavigationBar instead. 我正在组建一个应用程序,其中一些视图在常规UIViewControllers中并使用自己的UINavigationBar,而其他视图将是UINavigationController内部导航层次结构的一部分,并使用其UINavigationBar。

The problem is that the actual UINavigationBars that I'm seeing are different in these two cases. 问题是我看到的实际UINavigationBars在这两种情况下是不同的。 The ones in that use a UINavigationController's navigation bar seem unnaturally short. 使用UINavigationController的导航栏中的那些似乎不自然地短。 Here are some pictures, showing what I see in Interface Builder versus what I see at runtime. 下面是一些图片,展示了我在Interface Builder中看到的与我在运行时看到的内容。

常规视图控制器中的视图,具有自己的导航栏UINavigationController +内容视图控制器设置模拟器中的第一个视图在模拟器中的第二个视图 - 注意方式 - 太短的导航栏!

I can't figure out what I'm doing wrong here. 我无法弄清楚我在这里做错了什么。 Why are the navigation bars different heights? 为什么导航栏的高度不同? How can I make them the same? 我怎样才能使它们一样?

I truly believe you are mixing up two different features. 我真的相信你正在混合两种不同的功能。 UINaviationbar and UIToolbar. UINaviationbar和UIToolbar。 Here is apple's human guidelines. 这是苹果的人类指导方针。 Look at the top of the document for Navigation Bar 查看导航栏文档的顶部

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html#//apple_ref/doc/uid/TP40006556-CH13-SW41 http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html#//apple_ref/doc/uid/TP40006556-CH13-SW41

I will try to see if I can find a document on their dimensions. 我将尝试查看是否可以找到有关其尺寸的文档。 Here is one. 这是一个。 I am sure there are better ones available 我相信有更好的可用

http://www.idev101.com/code/User_Interface/sizes.html http://www.idev101.com/code/User_Interface/sizes.html

Figured out what was going on here. 弄清楚这里发生了什么。 UINavigationController resizes the navigation bar in portrait orientation automatically, and as far as I can tell, there is no built-in way to prevent this from happening. UINavigationController自动纵向调整导航栏的大小,据我所知,没有内置的方法可以防止这种情况发生。 However, with the help of this post ( How to prevent autoresizing UINavigationBar on Landscape ), I was able to get it working the way I wanted. 但是,借助这篇文章( 如何防止在横向上自动化UINavigationBar ),我能够以我想要的方式工作。 My code was slightly different from the code in the linked post. 我的代码与链接帖子中的代码略有不同。 I used: 我用了:

@implementation UINavigationBar (CustomHeight)

- (CGSize)sizeThatFits:(CGSize)size
{
    // Need to know the width of the window
    CGRect bounds = self.window.bounds;

    // Sometimes height and width are turned around. Take the largest of height and width as the real width.
    int width = (bounds.size.width > bounds.size.height) ? bounds.size.width : bounds.size.height;

    // Make sure that the 
    CGSize newSize = CGSizeMake(width, 44);
    return newSize;
}

I'm not sure if there's a better way to get the true width - the above is clunky but works for me since my app only runs in landscape. 我不确定是否有更好的方法来获得真正的宽度 - 以上是笨重的,但对我来说,因为我的应用程序只在景观中运行。 My bigger concern is that Apple says in its UINavigationBar documentation that you shouldn't directly adjust the frame. 我更关心的是Apple在其UINavigationBar文档中说你不应该直接调整框架。 But I guess I'm not directly doing that; 但我想我不是直接这样做的; presumably internal drawing methods will call this method and do their thing. 据推测,内部绘图方法会调用此方法并执行它们的操作。

Anyway, this works for me. 无论如何,这对我有用。 Hope it helps someone else. 希望它可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM