gpt4 book ai didi

ios - 导航栏和 View 的整个图像

转载 作者:行者123 更新时间:2023-11-29 03:07:44 24 4
gpt4 key购买 nike

我想要一个图像的矩形 (0, 0, 320, 64) 作为 UINavigationController 的 UINavigationBar 的背景,而图像的其余部分作为 UIViewController View 的背景。UINavigationController 的 UINavigationBar 的背景不应是透明的。因此,当用户向上 ScrollView 时,它应该位于 UINavigationBar 下方。

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

  • 在我的自定义 UINavgationController 类的 init 中:

    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,并且推送的一个 UIViewController 也包含多行 UILabel,并且两者的背景都是透明的,则交叉溶解动画将仅适用于 UILabels 的文本。看起来很糟糕。

所以我决定在

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

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

最佳答案

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

  • 在我的自定义 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];

关于ios - 导航栏和 View 的整个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22577831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com