创建一个UINavigationBar的分类
UINavigationBar+CustomHeight.h
@interface UINavigationBar (CustomHeight)
- (void)setHeight:(CGFloat)height;
@end
UINavigationBar+CustomHeight.m
#import "objc/runtime.h"
static char const *const heightKey = "Height";
@implementation UINavigationBar (CustomHeight)
- (void)setHeight:(CGFloat)height
{
objc_setAssociatedObject(self, heightKey, @(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)height
{
return objc_getAssociatedObject(self, heightKey);
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize;
if (self.height) {
newSize = CGSizeMake(self.superview.bounds.size.width, [self.height floatValue]);
} else {
newSize = [super sizeThatFits:size];
}
return newSize;
}
@end
使用方法
在-(void)viewWillAppear:(BOOL)animated方法里面添加
UINavigationBar *bar = self.navigationController.navigationBar;
[bar setHeight:88];
在-(void)viewWillDisappear:(BOOL)animated方法里面添加
UINavigationBar *bar = self.navigationController.navigationBar;
[bar setHeight:44];
这样我们就可以看到这样的界面了
what??导航栏的标题往下移动了?
这也许不是我们想要的界面。
我们可能需要调整导航栏的标题和按钮的位置了
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect rect = self.navigationController.navigationBar.frame;
UINavigationBar *bar = self.navigationController.navigationBar;
[bar setHeight:88];
//调整导航栏标题的位置
[bar setTitleVerticalPositionAdjustment:-44 forBarMetrics:UIBarMetricsDefault];
//调整导航栏按钮的位置
UIBarButtonItem *item = self.navigationItem.leftBarButtonItem;
[item setBackgroundVerticalPositionAdjustment:-44 forBarMetrics:UIBarMetricsDefault];
}