【问题标题】:Whole image for navigationBar and view导航栏和视图的整个图像
【发布时间】:2014-03-22 12:51:25
【问题描述】:

我希望将图像的矩形 (0, 0, 320, 64) 作为 UINavigationController 的 UINavigationBar 的背景,并将图像的其余部分作为 UIViewController 视图的背景。 UINavigationController 的 UINavigationBar 背景不应该是透明的。 因此,当用户向上滚动视图时,它应该位于 UINavigationBar 下方。

我已经通过以下方式解决了这个问题:

  • 在我的自定义 UINavgationController 类的初始化中:

    UIImage *backgroundTopRect = [UIImage imageNamed:@"backgroundTopRect"];
    [self.navigationBar setBackgroundImage:backgroundTopRect forBarMetrics:UIBarMetricsDefault];
    
    UIImage *backgroundRestRect = [UIImage imageNamed:@"backgroundRestRect"];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:backgroundRestRect]];
    
  • 在每个 UIViewController 的类的 viewDidLoad 中:

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

在 pushViewController:animated: 动画之后只移动文本。例如,如果一个 UIViewController 包含多行 UILabel 并且 push 一个也包含多行 UILabel 并且两者的背景都是透明的,则交叉溶解动画将仅适用于 UILabel 的文本。 看起来很糟糕。

所以我决定在

的帮助下替换默认动画
    -(id<UIViewControllerAnimatedTransitioning>) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC

问题是我说的对吗?还是有另一种正确的方法来达到我想要的?

【问题讨论】:

    标签: ios ios7 background uinavigationbar


    【解决方案1】:

    标准动画还有另一种解决方案:

    • 在我的自定义 UINavgationController 类的初始化中只设置了一个导航栏的背景:

      UIImage *backgroundTopRect = [UIImage imageNamed:@"backgroundTopRect"];
      [self.navigationBar setBackgroundImage:backgroundTopRect forBarMetrics:UIBarMetricsDefault];
      
    • 在每个 UIViewController 的类的 viewDidLoad 中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多