【问题标题】:Set background image for entire iOS app为整个 iOS 应用设置背景图片
【发布时间】:2013-12-18 19:39:58
【问题描述】:

我正在尝试按照以下建议为整个应用设置背景图片:set background image for entire iPhone / iPad app

但在 iOS 7 中(不知道其他版本)它根本不能正常工作。 我创建了一个简单的存储库,因此您可以更好地了解正在发生的事情。 过渡中存在一些小故障。

当您点击第一个视图中的一行时,第二个视图被推入导航控制器,但效果很奇怪。似乎行透明度起到了作用。 另一个问题是,当您返回前一个视图控制器时,会从导航堆栈中弹出视图控制器的细微阴影。正如我之前所说,您可以通过运行简单的 Xcode 项目来理解我的意思。

回购:https://github.com/socksz/FixedBackgroundImage

有什么想法吗?我已经尝试为每个控制器设置背景图像,但这不是我想要的,因为这样图像会“重叠”之前的背景图像,这不是预期的效果。

希望我解释清楚。

编辑 1

似乎问题是由于 iOS 7 管理两个视图控制器之间的转换的方式造成的。在您位于第二个视图控制器中并尝试使用滑动手势转到前一个控制器时,您可以看到当您开始手势时,第一个控制器出现在第二个控制器(您所看到的控制器)下方,并且由于 @ 987654324@s 有透明背景,你已经看到了第一个控制器。其实恐怕没有解决办法。很遗憾,如果不在每个控制器上设置背景图像,我就无法拥有固定的背景图像。

【问题讨论】:

  • 你看过appearance方法appearanceWhenContainedIn:吗?
  • @Popeye 用于什么目的?你能给我更多的细节吗?谢谢!
  • 使用appearanceWhenContainedIn: 将允许您为所有UITableViews 中包含的backgroundColor: 设置外观让我们说UIViewController class 或者您可以使用正常外观方法来设置backgroundImage: 是应用中每个 UIView 的背景。
  • 谢谢大力水手,但这不是我的问题。我想要实现的是有一个固定的背景图像,它保持在 tableview 下方。如果我使用 appearanceWhenContainedIn: 为 tableviews 设置背景图像,则与在每个视图控制器中设置背景图像相同。

标签: ios objective-c cocoa-touch ios7 uiimageview


【解决方案1】:

将此代码放入您的 appdelegate.m 文件 applicationDidFinishLaunching 方法中。

UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];   

     windowBackground=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background_window.png"]];
                windowBackground.frame=CGRectMake(0, 0, 320, 568);
                [window addSubview:windowBackground];
                [windowBackground release];

            window.frame = CGRectMake(0, 0, window.frame.size.width,568);

            [window addSubview:[navigationController view]];
            [window makeKeyAndVisible];

在每个 viewController 类的 viewDidLoad 方法中添加这段代码。

self.view.backgroundColor = [UIColor clearColor];

【讨论】:

    【解决方案2】:

    您只需在
    appdelegate.m 文件的 applicationdidFinishLaunchingWithOptions: 方法中写一行

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

    并在每个屏幕的 viewDidLoad 方法中放入以下行

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

    【讨论】:

    • 我不知道你发生了什么,但在我的 ios7 中这段代码有效
    • 你试过repo中的代码了吗?代码有效,我的意思是图像显示为背景。 “问题”是推/弹出控制器时的视觉效果
    • 查看已编辑的答案。现在它通过推/弹出控制器进行了测试,效果很好
    • 我也做过同样的事情。您介意将您的测试项目推送到 repo 或 zip 吗?
    • 如果对您有帮助,请接受我的回答,不要忘记Up My Ans
    【解决方案3】:

    将此代码写入 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法

     UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"YourImageName.png"]];
     self.window.backgroundColor = background;
    

    【讨论】:

      【解决方案4】:

      我在 iPhone 应用程序中需要根据用户的偏好设置页面的背景图像。我处理它的方式是添加一个UIImageView 和背景图像作为视图的子视图,就像这样 -

      UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
      bgImageView.frame = self.view.bounds;
      [self.view addSubview:bgImageView];
      [self.view sendSubviewToBack:bgImageView];
      

      我克隆了您的 Github 存储库,并将上述代码添加到两个视图控制器的 viewDidLoad 中。我还在相同的方法中添加了以下代码行 -

      self.tableView.opaque = NO;
      

      我在didFinishLaunchingWithOptions 中注释掉了您设置背景颜色的代码。通过这些更改,在视图控制器之间导航时的工件消失了。我使用 iPhone Retina(3.5 英寸)和 iPhone Retina(4 英寸)模拟器进行了测试。

      在故事板中导航往返ViewController 时看到工件的原因需要进行一些调查。我的建议可能会或可能不会满足您的要求,但是,您可以尝试将此作为解决方案。

      附:该方法需要对自动布局约束进行一些调整。

      【讨论】:

        【解决方案5】:

        迟到的帖子...

        如果您使用的是 NavigationController,您可以尝试覆盖 TopViewController “get”部分以自动将 BackGroundColor 设置为您的图像。 Appologies,我们使用 Xamarin,它为我们从 C# 转换为目标 C(不确定具体的语法)。在 C# 中,它在 NavigationController 类中看起来像这样。

            public override UIViewController TopViewController
        {
          get
          {
            if (base.TopViewController.View.BackgroundColor != "Your Image")
            {
              base.TopViewController.View.BackgroundColor = "Your Image";
            }
            return base.TopViewController;
          }
        }
        

        【讨论】:

          【解决方案6】:

          我没有找到将其放在全局范围内的方法。但是,您可能会发现这对静态/固定图像很有用(而不是在设置 backgroundColor 属性时获得的运动图像),请为每个屏幕使用 backgroundView 属性。

          self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
          

          我自己创建了一个 UtilTableViewController 来完成我需要它做的所有主题和自定义的事情,把这段代码放在那里,然后对我的所有视图进行子类化。它不是全局设置的图像,但我只需设置一次,我的所有 TableView 都会使用它。

          【讨论】:

            【解决方案7】:

            您可以通过下面的代码设置背景图片...我们可以将此代码放在viewcontroller.m文件中的viewdidload方法中

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-09-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-06-22
              • 1970-01-01
              • 1970-01-01
              • 2023-03-25
              相关资源
              最近更新 更多