简体   繁体   English

带有扩展导航栏的iOS过渡

[英]iOS transition with extended navigation bar

I am using an extended navigation bar for my application, increasing its height by 30 points. 我正在为我的应用程序使用扩展的导航栏,将其高度增加了30点。 To do that, I have subclassed UINavigationBar, here is my CustomNavigationBar.m : 为此,我将UINavigationBar子类化,这是我的CustomNavigationBar.m:

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

Then, in the viewDidLoad of a view controller, I set up the titleView property of my navigation item like this: 然后,在视图控制器的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;

and the frame of my titleView has a height of 80 points. 我的titleView框架的高度为80点。

Everything works fine until then, but I have an issue when I change view controller (the image used changes between view controllers). 到那时为止,一切正常,但是当我更改视图控制器时(在所使用的图像在视图控制器之间更改时)出现问题。 As soon as the animation starts, the bottom of my image (and just my image in the titleView, not the whole navigation bar) gets cropped by my extra height, the transition takes place correctly (well, only with the remaining bit), and as soon as it is finished, the cropped bit of the new image appears. 动画一开始,我的图像底部(以及titleView中的图像,而不是整个导航栏)就被我的额外高度裁剪了,过渡正确地进行了(嗯,只剩下了一点),并且完成后,将出现新图像的裁剪部分。

I hope it is clear, it seems like I can't get any animation on the extra bit of navigation bar that I have added. 我希望很清楚,似乎我无法在添加的导航栏上看到任何动画。

Thanks for your help, I really don't know where to start with this. 感谢您的帮助,我真的不知道从哪里开始。

Instead of subclassing the UINavigationBar try customizing it. 与其对UINavigationBar进行子类化,不如尝试对其进行自定义。 It will require you to set the constraints in Interface Builder to offset Navigation Bar sizing, but it's not too difficult: 它将需要您在Interface Builder中设置约束以抵消Navigation Bar的大小,但这并不困难:

#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;
}

Sample method to call the back button: 调用后退按钮的示例方法:

- (void)backButtonPressed
{
    for (UIViewController *vc in self.childViewControllers)
    {
        if ([vc isEqual:[PinningVC class]])
        {
            [vc removeFromParentViewController];
        }
    }

    [self.navigationController popViewControllerAnimated:NO];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM