简体   繁体   中英

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:

  • in init of my custom UINavgationController class:

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

     [self.view setBackgroundColor:[UIColor clearColor]]; 

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?

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]; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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