ios 更改 UINavigationBar 高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894985/
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
Change UINavigationBar Height
提问by Shimon Wiener
Can some one tell me how to change the navigation bar height?
有人可以告诉我如何更改导航栏高度吗?
Here is what i have so far:
这是我到目前为止所拥有的:
CGFloat navBarHeight = 10;
self.navigationController.navigationBar.frame.size.width = navBarHeight;
回答by Shimon Wiener
You can create a category class based on UINavigationBar
like this
您可以基于UINavigationBar
这样创建一个类别类
.h file
.h 文件
#import UIKit/UIKit.h
@interface UINavigationBar (myNave)
- (CGSize)changeHeight:(CGSize)size;
@end
and .m file
和 .m 文件
#import "UINavigationBar+customNav.h"
@implementation UINavigationBar (customNav)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(320,100);
return newSize;
}
@end
It works very nice.
它很好用。
回答by Gil Margolin
You should not change the height of the navigation bar. From Apple Programing Guide on View Controller:
您不应更改导航栏的高度。来自视图控制器上的 Apple 编程指南:
Customizing the Navigation Bar Appearance
自定义导航栏外观
In a navigation interface, a navigation controller owns its UINavigationBar object and is responsible for managing it. It is not permissible to change the navigation bar object or modify its bounds, frame, or alpha values directly. However, there are a few properties that it is permissible to modify, including the following:
在导航界面中,导航控制器拥有它的 UINavigationBar 对象并负责管理它。不允许直接更改导航栏对象或修改其边界、框架或 alpha 值。但是,有一些属性是允许修改的,包括:
● barStyle property
● barStyle 属性
● translucent property
● 半透明属性
● tintColor property
● tintColor 属性
(taken from Apple: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html)
(取自苹果:https: //developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html)
-- UPDATE -- IOS 7 --- still only the available properties can be changed but below is a great tutorial on how to achieve flexibility in the navigation bar http://www.appcoda.com/customize-navigation-status-bar-ios-7/
-- 更新 -- IOS 7 --- 仍然只能更改可用的属性,但下面是有关如何在导航栏中实现灵活性的精彩教程http://www.appcoda.com/customize-navigation-status-bar -IOS 7/
回答by Yerkezhan
I have solved this issue, I created the subclass of UINavigationController. In this MainNavigationViewController.m file I rewrite viewDidLayoutSubviews() method, where I set the frame for self.navigationBar
我已经解决了这个问题,我创建了 UINavigationController 的子类。在这个 MainNavigationViewController.m 文件中,我重写了 viewDidLayoutSubviews() 方法,在那里我为 self.navigationBar 设置了框架
回答by ghr
sizeThatFits: no longer works with iOS11 UINavigationBar subclasses. Apple Engineering have confirmed that this technique was never supported and with iOS11 changes sizeThatFits: is no longer consulted.
sizeThatFits:不再适用于 iOS11 UINavigationBar 子类。Apple Engineering 已确认此技术从未得到支持,并且随着 iOS11 的更改 sizeThatFits: 不再被咨询。
回答by Augustine P A
Override -(void)viewWillAppear method in the viewController and Try the code below,
覆盖 viewController 中的 -(void)viewWillAppear 方法并尝试下面的代码,
- (void)viewWillAppear {
UINavigationBar *navigationBar = [[self navigationController] navigationBar];
CGRect frame = [navigationBar frame];
frame.size.height = 82.0f;
[navigationBar setFrame:frame];
}
回答by Cliff Viegas
Try using -
尝试使用 -
CGFloat navBarHeight = 10.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
回答by Beninho85
Most Simple and Effective Solution is to add a category to navigation bar. (but maybe not the best solution)
最简单有效的解决方案是在导航栏中添加一个类别。(但也许不是最好的解决方案)
@interface UINavigationBar (CustomHeight)
@end
@implementation UINavigationBar (CustomHeight)
- (CGSize)sizeThatFits:(CGSize)size {
CGRect screenRect = [[UIScreen mainScreen] bounds];
return CGSizeMake(screenRect.size.width, kNavBarHeightNoStatus);
}
@end
knavBarHeightNoStatus is the height of your navbar. We use UIScreen bounds because frame of UINavigationBar is wrong
knavBarHeightNoStatus 是导航栏的高度。我们使用 UIScreen bounds 因为 UINavigationBar 的 frame 是错误的
PS: Be careful, you can have some issues using it with a SideMenu
PS:小心,使用 SideMenu 可能会遇到一些问题
回答by tachikoma
self.navigationController.navigationBar.frame
is a readonly
property on iOS version 4.x
self.navigationController.navigationBar.frame
是readonly
iOS 版本 4.x 上的一个属性
it will be changed even though, you will see abnormal behavior.
它会被改变,即使你会看到异常行为。
for example, when app state changed to background and get back to foreground the Navigation bar will show original sized height
例如,当应用程序状态更改为后台并返回前台时,导航栏将显示原始大小的高度
回答by Josh Gafni
I ran into a lot of problems depending on the method chosen. In my specific case, the navigation bar was hidden throughout the app except in the Settings View Controller. As I needed to specify a custom height of the navigation bar and show the navigation bar at the same time, the segue between the source view controller and the Settings View Controller had a very sloppy animation.
根据选择的方法,我遇到了很多问题。在我的特定情况下,导航栏隐藏在整个应用程序中,除了设置视图控制器中。由于我需要指定导航栏的自定义高度并同时显示导航栏,因此源视图控制器和设置视图控制器之间的转场动画非常草率。
The absolute best solution can be found here: https://gist.github.com/maciekish/c2c903d9b7e7b583b4b2. I just imported the category into the Settings View Controller, and in viewDidLoad
called the setHeight:
method on `self.navigationController.navigationBar'. Looks great!
绝对最好的解决方案可以在这里找到:https: //gist.github.com/maciekish/c2c903d9b7e7b583b4b2。我刚刚将类别导入到设置视图控制器中,并viewDidLoad
调用了setHeight:
“self.navigationController.navigationBar”上的方法。看起来很棒!
回答by abdul sathar
Following code won't affect other parameters in frame except height
以下代码不会影响框架中除高度以外的其他参数
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, 150)];
if you want change other parameters means, just edit the any one of the parameters....
如果你想改变其他参数意味着,只需编辑任何一个参数....