我想制作一个更高的导航栏,我正在尝试使用 UINavigationBar 的子类来做到这一点.
I want to make a taller navigation bar and I am trying to do this using a subclass of UINavigationBar.
这是我的 UINavigationBar 的子类:
Here is my subclass of UINavigationBar:
import UIKit
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(size.width, 87)
return newSize
}
}
在我嵌入导航控制器的 ViewController 中,我在 viewDidLoad() 函数中编写了以下代码
And in my ViewController which is embedded in a navigation controller, I write the following code in the viewDidLoad() function
self.navigationController!.setValue(TallerNaviBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 87)), forKeyPath: "navigationBar")
没有报告错误,但运行代码时我什么也没得到.一个完全空白的视图..
There is no error reported, but I get nothing when I run the code. A completely blank view. .
有什么建议吗?或者其他改变高度的方法?谢谢.
Any suggestion? Or some other way of changing the height? Thanks.
iOS 11 及更高版本更新
苹果不想让你弄乱导航栏的高度,所以不要碰它见这里:https://openradar.appspot.com/32912789在这里:https://forums.developer.apple.com/thread/88202#274620
apple doesn't want you messing with the navigation bar height, so don't touch it see here: https://openradar.appspot.com/32912789 and here: https://forums.developer.apple.com/thread/88202#274620
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(self.superview!.frame.size.width, 87)
return newSize
}
}
我不会快速,但答案很简单,这里是 ObjC
I don't do swift, but the answer is easy, here's ObjC
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake([self superview].frame.size.width, 40);
}
这是我对 Swift 的解释:
Here's my interpretation in Swift:
import UIKit
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(superview.width, 87)
return newSize
}
}
你会遇到的问题不是这个方法,这是简单的部分,问题是强制导航控制器总是使用这个导航栏
The problem you will have isn't with this method, this is the easy part, the problem is forcing the navigation controller to always use this navigation bar
我对 IOS 中的所有内容进行子类化和调整大小,包括导航控制器和 tabbarcontrollers,为了强制所有导航控制器使用此导航栏,您必须子类化一个 navigationController,并且仅在整个应用程序中使用此导航控制器,这是子类的工作方式,这是 Obj C,所以你必须翻译它:
I subclass and resize everything in IOS, including navigation controllers and tabbarcontrollers, in order to enforce that all navigation controllers use this navigation bar, you must subclass a navigationController and only use this navigationcontroller throughout your app, here's how the subclass works, this is Obj C, so you'll have to translate it:
@interface NSHNavigationController () <UINavigationBarDelegate, UINavigationControllerDelegate>
{
}
@implementation NSHNavigationController
#pragma mark Initialization
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
{
self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:toolbarClass];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}
- (void)dealloc
{
[self setInternalDelegate:nil];
[self setExternalDelegate:nil];
}
#pragma mark Setup
- (void)NSHSetupNavigationController
{
[self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
}
这条线将为您完成:
[self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
哦,是的,请确保您将导航栏子类化,您说过您是,但您是这样做的,很简单:
Oh yeah, and make sure you are subclassing the nav bar, you said you were, but here's how you do it, it's simple:
#import "NSHNavigationBar.h"
@implementation NSHNavigationBar
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:fontMagicForRegularNSHFont};
[self setTitleTextAttributes:attributes];
[self setTranslucent:true];
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake([self superview].frame.size.width, heightResizer(40));
}
- (void)layoutSubviews {
[super layoutSubviews];
}
因此,总而言之,将 UINavigationBar 和 UINavigationController 子类化并设置好,这将允许您随时操作导航栏,您也可以键入转换视图控制器的导航栏,这有点疯了,会迷惑很多人,但这里是这样的:
So, in summary, subclass both the UINavigationBar and the UINavigationController and you are set, this will allow you to manipulate the navigation bar whenever you'd like, you can also type cast your view controller's navigation bars, this is a little nuts and will confuse a lot of people, but here it goes:
-(CustomNavigationBar *)navBar {
return (id)[self.navigationController navigationBar];
}
把上面的东西放在你的视图控制器中,然后你这样称呼它:
put the stuff above in your view controller and then you call it like this:
[[self navBar] setBackGroundColor:[UIColor blueColor]];
这将成功地将您的导航控制器类型转换为您的自定义导航栏,如果您想从这里更改导航栏的高度,那么您可以执行以下操作:
This will successfully typecast your navigationcontroller to be your custom navigation bar, if you want to change the height of the nav bar from here, then you can do something like this:
这篇关于以编程方式快速更改导航栏的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!