0

I would like to place an absolute background image for my app that does not move no matter what.

I would like it to seem as though my segues simply move the destinationViewController's views onto this background, as if it were one page.

I have seen this answer, set background image for entire iPhone / iPad app, but as stated, colorWithPatternImage devours memory.

I would like an answer in Objective-C and Swift please.

Thanks

Community
  • 1
  • 1
Eric Johnson
  • 1,087
  • 1
  • 11
  • 16

1 Answers1

1

The simplest way is to add a UIImageView to your root window and make sure all of your other views have transparent backgrounds;

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let window=self.window!

    let backgroundView=UIImageView(frame: window.frame)

    backgroundView.image=UIImage(named: "Background.JPG")!

    window.addSubview(backgroundView)

    return true
}

or in Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

    UIWindow *window=application.windows.firstObject;
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:window.frame];

    imageView.image=[UIImage imageNamed:@"Background.jpg"];

    [window addSubview:imageView];
    return YES;
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I believe this smokes my memory as well but I'll accept it – Eric Johnson Feb 22 '16 at 03:01
  • 1
    @EricJohnson in other options you can create a baseViewController with background and inherit all viewcontroller with transperant color from that baseViewController – HardikDG Feb 22 '16 at 03:12
  • 2
    That will result in a consistent background, but as each view controller has its own background image, the image will still move during view controller transitions. – Paulw11 Feb 22 '16 at 03:13
  • 1
    @EricJohnson Memory use will depend on your image. For example for my test app, base memory usage is about 22MB. With a 6.8MB high-res JPEG, this rise to about 90MB - Whether the image is in the window or a UIImageView on the actual VC. With a 800Kb image, usage is about 24MB. Since the image has to be uncompressed and rendered as a full bitmap, larger images will use more memory, regardless of how they are shown – Paulw11 Feb 22 '16 at 03:23