导航栏和视图的整个图像

Posted

技术标签:

【中文标题】导航栏和视图的整个图像【英文标题】: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

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

【问题讨论】:

【参考方案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]; 

【讨论】:

以上是关于导航栏和视图的整个图像的主要内容,如果未能解决你的问题,请参考以下文章

图像视图在滚动视图中隐藏导航栏

隐藏的导航栏和更改的应用程序流程 - swift

导航栏和视图之间的分隔符 - iOS 7

如何将导航栏和标签栏设置为通过 UIPopoverViewController 的视图?

iOS 8 启动图像文件 - 导航栏和状态栏

如何添加覆盖所有其他视图(包括导航栏和状态栏)的暗屏?