iPhone - 如何设置 uinavigationbar 高度?
我想让导航视图的顶部小一点。你将如何实现这一目标?这是我到目前为止所尝试过的,但正如你所看到的,即使我使导航栏变小,它曾经占据的区域仍然存在(黑色)。
[window addSubview:[navigationController view]];
navigationController.view.frame = CGRectMake(0, 100, 320, 280);
navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 20);
navigationController.view.backgroundColor = [UIColor blackColor];
[window makeKeyAndVisible];
I want to make the top of the navigation view a bit smaller. How would you achieve this? This is what I've tried so far, but as you can see, even though I make the navigationbar smaller, the area which it used to occupy is still there (black).
[window addSubview:[navigationController view]];
navigationController.view.frame = CGRectMake(0, 100, 320, 280);
navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 20);
navigationController.view.backgroundColor = [UIColor blackColor];
[window makeKeyAndVisible];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
创建一个具有自定义 sizeThatFits 的 UINavigationBar 类别。
Create a UINavigationBar Category with a custom sizeThatFits.
使用这个导航栏子类,我在 iPad 上的 iOS 5.x 到 iOS 6.x 上成功创建了一个更大的导航栏。这给了我一个更大的导航栏,但不会破坏所有动画。
Using this navigation bar subclass I've successfully created a larger navigation bar on iOS 5.x to iOS 6.x on the iPad. This gives me a larger navigation bar but doesn't break all the animations.
为了快速
创建 Uinavigation bar 的子类。
两侧都会有两个空白条,我将宽度更改为确切的数字以使其正常工作。
然而,标题和后退按钮与底部对齐。
For swift
create a subclass of Uinavigation bar.
There will be two blank strips on both sides, I changed the width to the exact number to make it work.
However the title and back button are aligned to the bottom.
没有必要对 UINavigationBar 进行子类化。在 Objective-C 中,您可以使用类别,在 Swift 中,您可以使用扩展。
It's not necessary to subclass the UINavigationBar. In Objective-C you can use a category and in Swift you can use an extension.
我发现以下代码在 iPad(和 iPhone)上表现更好:
I have found the following code to perform better on iPad (and iPhone):
如果您想为导航栏使用自定义高度,我认为您至少应该使用自定义导航栏(而不是导航控制器中的导航栏)。隐藏 navController 的栏并添加您自己的栏。然后你可以将其高度设置为你想要的任何高度。
If you want to use a custom height for your nav bar, I think you should probably, at the very least, use a custom nav bar (not one in your nav controller). Hide the navController's bar and add your own. Then you can set its height to be whatever you want.
我能够在 Swift 中使用以下子类代码。它使用现有高度作为起点并添加到它。
与本页上的其他解决方案不同,在横向和纵向之间切换时,它似乎仍然可以正确调整大小。
I was able to use the following subclass code in Swift. It uses the existing height as a starting point and adds to it.
Unlike the other solutions on this page, it seems to still resize correctly when switching between landscape and portrait orientation.
这是 Swift 中一个非常好的子类,您可以在 Storyboard 中配置它。它基于 mackross 所做的工作,这很棒,但它是 iOS7 之前的版本,会导致您的导航栏无法延伸到状态栏下方。
Here's a pretty nice subclass in Swift that you can configure in Storyboard. It's based on the work done by mackross, which is great, but it was pre-iOS7 and will result in your nav bar not extending under the status bar.
我还是ios新手。我通过以下方式解决了问题:
我创建了一个继承自 UINavigationBar 的新类
我重写了以下方法:
3.为了获取导航栏的自定义背景,我重写了以下方法:
I am a newbie in ios yet. I solved the problem in following way :
I have created a new class that inherits from UINavigationBar
I override the following method :
3.To get a custom background of the navigation bar, I overrided the following method :