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

标签 ios ios7 background uinavigationbar

我想要一个图像的矩形 (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/

相关文章:

ios - iOS Amazon Cognito登录错误InvalidParameterException

ios - 具有模糊背景的模态导航 Controller ,在推送动画期间无缝?

angular - ngStyle 背景图像不可见

jquery:获取和设置图像,从背景CSS重复

ios - 如何将 UIImageView 设置为手机壁纸

objective-c - 由于 ios 和 iphonesimulator 的库不同而导致链接错误?

OSX El Capitan 上的 Xcode 7.1,iOS 7 模拟器 - 它应该可以工作吗?

ios - iOS 7 上的分组 UITableView 故障

ios - 如何在我的 iOS 应用程序中获得透明背景,以便我可以看到主屏幕壁纸?

java - 添加 JLabel 作为 JPanel 的背景