【问题标题】:set background image for entire iPhone / iPad app为整个 iPhone / iPad 应用程序设置背景图像
【发布时间】:2011-06-22 09:02:05
【问题描述】:

我想要一张图片作为我的应用的背景,无论它们在什么视图控制器上 - 你是如何做到这一点的?

【问题讨论】:

    标签: iphone objective-c ipad


    【解决方案1】:

    取决于你有什么样的界面。标签?基于导航?但一般的答案是:在您的主视图之前/下方添加一个 UIImageView 到您的 UIWindow 。然后让你的主视图控制器处理的每个视图都有一个透明的背景。如果不知道您是否使用 IB,或者您的视图层次结构是什么样的,很难给出更具体的建议。

    【讨论】:

    • 基于导航,因此当视图控制器被推送和弹出时,背景保持不变
    • 结合 sudo 和 Manni 的建议可以轻松完成您的需要:将窗口上的 backgroundColor 设置为图像 (colorWithPatternImage:) 并使导航控制器及其所有子控制器上的 backgroundColor 透明.您可能也必须在它们上设置 opaque = NO 。请注意,这可能会影响动画期间的性能,但在较新的设备上应该不是问题。
    【解决方案2】:

    以下是为图像设置背景的方法:

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
    

    编辑:要写下 Felixyz 所说的话(感谢 Manni),请在您的代表中执行此操作:

    window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
    

    在您想要拥有图像的每个视图中,执行以下操作:

    self.view.backgroundColor = [UIColor clearColor];
    

    【讨论】:

    • 我最终在主窗口笔尖上使用了 UIImageView,在我的视图上使用了 UIColor clearColor。请注意,使用上面所说的 colorWithPatternImage 使我的内存使用量在首次加载应用程序时从 1.7M 跃升至 9.5M - colorWithPatternImage 似乎只是消耗了大量内存。
    • 建议为你的 Universal 应用准备 3 个 png 文件,分别为 Background.png、Background@2x.png、Background~iPad.png,并使用以下代码设置背景:self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background"]]; iOS 将根据您的设备类别选择正确的 png 文件。希望对您有所帮助。
    • 我在基于UINavigation 的应用程序中尝试了上述方法。这种方法的问题是在window.background 上设置图像正确设置图像但使其他视图清晰颜色不好因为在推送或弹出视图重叠时,您可以看到推送到堆栈中的视图。知道如何修复它
    • 我通过将背景颜色设置为导航控制器视图背景,解决了 Aiden 的问题。然后,当我使用 Storyboards 时,我将使用自定义 UIStoryboardSegue 替换标准推送动画(带有滑动视图),该 UIStoryboardSegue 实现了褪色效果:图像保持稳定并且 Aiden 感叹的效果不再可见。就我而言,我也没有使用导航栏,它们都是隐藏的(模式视图除外)。
    • @viggio24 如何准确地创建“实现淡入淡出效果的自定义 UIStoryboardSegue”?
    【解决方案3】:

    在我的应用程序中,我设置了默认背景颜色。也许你可以用你的背景图片来做这个:

    1.:在 AppDelegate 中设置 UIWindow 的背景颜色:

    window.backgroundColor = [UIColor myBackgroundGray]; // own Category
    

    2.:现在,让所有其他视图透明:

    self.view.backgroundColor = [UIColor clearColor]; // = transparent
    

    【讨论】:

    • 这会在使用动画呈现模态演示者时导致用户体验不佳。您可以从模态视图和现有视图中看到控件,然后现有视图消失。
    【解决方案4】:

    您的背景是任何继承视图的对象的属性。例如,标签、按钮、控制器和应用程序窗口都有背景。如果您希望它完全成为整个应用程序的背景,您必须爬上控制器中的路径以找到非常“顶部”(底部视图)的视图,并将其背景设置为您想要的图像。

    【讨论】:

      【解决方案5】:

      在你的 AppDelegate 中

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      

      添加这一行:

      [self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
      

      您只需将视图背景设置为[UIColor clearColor];

      【讨论】:

        【解决方案6】:

        我不确定性能影响,但我需要完成类似的事情,并最终使用了一个运行良好的 UIImageView(在 C# 中,但在 obj-c 中的工作方式相同):

        //Add the view controller first, to ensure proper order of views later.
        Window.RootViewController = new UIViewController();
        
        //create backdrop image view
        var imageView = new UIImageView(Window.Bounds);
        imageView.Image = UIImage.FromBundle("backdrop.jpg");
        
        //insert into window.
        Window.InsertSubview(imageView, 0);
        

        这不处理方向变化,但在我的情况下,允许我向背景添加运动效果(例如视差)。

        【讨论】:

          【解决方案7】:

          我通常使用这个功能来避免与iphone上的导航栏重叠。

          -(void)setImageBackground:(NSString*)imageName{
              UINavigationController* navigationController = [self navigationController];
              float height = navigationController.toolbar.frame.size.height;
              CGSize size = self.view.frame.size;
              size.height = size.height;
              UIGraphicsBeginImageContext(size);
              CGRect bounds = self.view.bounds;
              bounds.origin.y = bounds.origin.y + height;
              bounds.size.height = bounds.size.height-height;
              [[UIImage imageNamed:imageName] drawInRect:bounds];
              UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext();
              self.view.backgroundColor = [UIColor colorWithPatternImage:image];
          }
          

          【讨论】:

            【解决方案8】:

            只需在viewDidLoad 中调用此assignbackground

            override func viewDidLoad() {
            assignbackground()
            }
            
            func assignbackground(){
                    let background = UIImage(named: "background")
            
                    var imageview : UIImageView!
                    imageview = UIImageView(frame: view.bounds)
                    imageview.contentMode =  UIViewContentMode.ScaleAspectFill
                    imageview.clipsToBounds = true
                    imageview.image = background
                    imageview.center = view.center
                    view.addSubview(imageview)
                    self.view.sendSubviewToBack(imageview)
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-19
              • 2019-07-11
              • 1970-01-01
              • 2013-12-18
              • 1970-01-01
              相关资源
              最近更新 更多