带扩展导航栏的iOS转换
我正在为我的应用程序使用一个扩展的导航栏,将其高度增加了30个点。 为此,我对UINavigationBar进行了子类化,下面是我的CustomNavigationBar.m:带扩展导航栏的iOS转换,ios,objective-c,Ios,Objective C,我正在为我的应用程序使用一个扩展的导航栏,将其高度增加了30个点。 为此,我对UINavigationBar进行了子类化,下面是我的CustomNavigationBar.m: CGFloat CustomNavigationBarHeightIncrease = 30.f; @implementation CustomNavigationBar - (CGSize)sizeThatFits:(CGSize)size { CGSize amendedSize = [super size
CGFloat CustomNavigationBarHeightIncrease = 30.f;
@implementation CustomNavigationBar
- (CGSize)sizeThatFits:(CGSize)size {
CGSize amendedSize = [super sizeThatFits:size];
amendedSize.height += CustomNavigationBarHeightIncrease;
return amendedSize;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize {
[self setTransform:CGAffineTransformMakeTranslation(0, -(CustomNavigationBarHeightIncrease))];
}
- (void)layoutSubviews {
[super layoutSubviews];
NSArray *classNamesToReposition = @[@"_UINavigationBarBackground"];
for (UIView *view in [self subviews]) {
if ([classNamesToReposition containsObject:NSStringFromClass([view class])]) {
CGRect bounds = [self bounds];
CGRect frame = [view frame];
frame.origin.y = bounds.origin.y + CustomNavigationBarHeightIncrease - 20.f;
frame.size.height = bounds.size.height + 20.f;
[view setFrame:frame];
}
}
}
@end
然后,在视图控制器的viewDidLoad中,我设置了导航项的titleView属性,如下所示:
self.navigationItem.title = @"";
UIImage* logoImage = [UIImage imageNamed:@"welcome"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logoImage];
self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFill;
self.navigationItem.titleView.contentMode = UIViewContentModeBottom;
我的标题视图的框架有80个点的高度
在那之前一切都很好,但是当我更改视图控制器时,我遇到了一个问题(使用的图像在视图控制器之间更改)。动画一开始,我的图像的底部(标题视图中只有我的图像,而不是整个导航栏)就会被我的额外高度裁剪,过渡就会正确发生(好的,只有剩余的部分),一旦完成,新图像的裁剪部分就会出现
我希望这是清楚的,似乎我无法在我添加的额外导航栏上获得任何动画
谢谢你的帮助,我真的不知道从哪里开始 不要对UINavigationBar进行子类化,而是尝试对其进行自定义。这将要求您在Interface Builder中设置约束以偏移导航栏大小,但这并不太困难:
#pragma mark - Navigation Bar Setup
- (void)setupNavBar
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
[self.navigationController.navigationBar setBounds:CGRectMake(0, 0, self.view.frame.size.width, 80)];
// Customizing the back button in the Nav Bar
self.navigationItem.hidesBackButton = YES;
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 30, 30);
[backButton setImage:[UIImage imageNamed:@"backbtn"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = leftButton;
UIImage *title = [UIImage imageNamed:@"title_image"];
UIImageView *titleImgView = [[UIImageView alloc] initWithImage:title];
titleImgView.frame = CGRectMake(10, 15, 85, 24);
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 105, 50)];
[titleView addSubview:titleImgView];
self.navigationItem.titleView = titleView;
}
回调按钮的示例方法:
- (void)backButtonPressed
{
for (UIViewController *vc in self.childViewControllers)
{
if ([vc isEqual:[PinningVC class]])
{
[vc removeFromParentViewController];
}
}
[self.navigationController popViewControllerAnimated:NO];
}
确保在显示屏幕之前调用这些方法-在
视图中将出现或viewDidLoad
。