繁体   English   中英

带有扩展导航栏的iOS过渡

[英]iOS transition with extended navigation bar

我正在为我的应用程序使用扩展的导航栏,将其高度增加了30点。 为此,我将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

然后,在视图控制器的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;

我的titleView框架的高度为80点。

到那时为止,一切正常,但是当我更改视图控制器时(在所使用的图像在视图控制器之间更改时)出现问题。 动画一开始,我的图像底部(以及titleView中的图像,而不是整个导航栏)就被我的额外高度裁剪了,过渡正确地进行了(嗯,只剩下了一点),并且完成后,将出现新图像的裁剪部分。

我希望很清楚,似乎我无法在添加的导航栏上看到任何动画。

感谢您的帮助,我真的不知道从哪里开始。

与其对UINavigationBar进行子类化,不如尝试对其进行自定义。 它将需要您在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;
}

调用后退按钮的示例方法:

- (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