blyabtroi
blyabtroi

Reputation: 2076

Whole image for navigationBar and view

I want to have a rect (0, 0, 320, 64) of an image as a background for UINavigationController's UINavigationBar and the rest of the image as a background of UIViewController's view. Background for UINavigationController's UINavigationBar shouldn't be transparent. So when user scrolls a view up, it should go under the UINavigationBar.

I've resolved this the following way:

After that animation of pushViewController:animated: moves only text. For example, if one UIViewController contains multiline UILabel and pushed one contains multiline UILabel too and backgrounds of both are transparent, cross dissolve animation would apply only to UILabels' text. It looks bad.

So I've decided to replace default animation with help of

    -(id<UIViewControllerAnimatedTransitioning>) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC

The question is am I right? Or is there another correct way to reach what I want?

Upvotes: 0

Views: 60

Answers (1)

blyabtroi
blyabtroi

Reputation: 2076

There is another solution with a standard animation:

  • in init of my custom UINavgationController class set only a navigationBar's background:

    UIImage *backgroundTopRect = [UIImage imageNamed:@"backgroundTopRect"];
    [self.navigationBar setBackgroundImage:backgroundTopRect forBarMetrics:UIBarMetricsDefault];
    
  • in viewDidLoad of every UIViewController's class:

    CGRect rect = [[UIScreen mainScreen] bounds];
    rect.origin.y -= NAVIGATION_BAR_HEIGHT; //=64
    UIView *imageView = [[UIView alloc] initWithFrame:rect]; //we should use UIView, which setBackgroundColor can reproduce image or make tiled image
    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth; //it's needed if you change orientation
    [imageView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Background"]]];
    [self.view addSubview:imageView];
    [self.view sendSubviewToBack:imageView]; 
    

Upvotes: 1

Related Questions