Pranav Wadhwa
Pranav Wadhwa

Reputation: 7736

Sharing a Background Image Across Multiple View Controllers

I am building an app in Swift using Xcode. I have a set of 5 images which I want to use as a background on all of my view controllers.

I have 10+ View Controllers, but I want to be able to reuse the images.

Generally I would add an image view to all of my view controllers, and set the image.

I would use a public variable to change what image is being shown. But on this app, I have many view controllers, and it wouldn't be very efficient to create each and every image view.

Is there a way I can do it faster? Thanks.

Upvotes: 1

Views: 2047

Answers (3)

Satheesh
Satheesh

Reputation: 11276

A better way of doing this with swift when compared to subclassing, write an extension and call your method in all your view controllers.

extension UIViewController{

func setCustomBackgroundImage(){

    self.view.backgroundColor = UIColor(patternImage: UIImage(named:"yourImageFromXcAssets")!)

    //or

    let bgImage = UIImageView(image: UIImage(named: "yourImageFromXcAssets"))
    self.view.addSubview(bgImage)
}
}

and in your view controllers just call it like regular UIViewController class methods,

class ViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    self.setCustomBackgroundImage()
}

or write a category over UIViewController with Objective-C

#import <UIKit/UIKit.h>

@interface UIViewController (customBackground)

- (void)setCustomBackgroundImage;

@end

#import "UIViewController+customBackground.h"

@implementation UIViewController (customBackground)

- (void)setCustomBackgroundImage{
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageFromXcAssets"]]];
    //or
    UIImageView *bgImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourImageFromXcAssets"]];
    bgImage.frame = self.view.frame;
    [self.view addSubview:bgImage];
}

@end

And the same approach with Objective C as well,

 [self setCustomBackgroundImage]; 

I am using a view controller extension to manage all my styles, colors and appearances across my app.

Upvotes: 1

Tim
Tim

Reputation: 2098

Create a set of global variables in a class:

class Images {
    static let image1 = UIImage(named: "image1.png")
    static let image2 = UIImage(named: "image2.png")
    static let image3 = UIImage(named: "image3.png")
    static let image4 = UIImage(named: "image4.png")
    static let image5 = UIImage(named: "image5.png")

    static var selectedImage = image1
}

Now to select the image,

Images.selectedImage = Images.image3

To set background, in each VC, set:

imageView.image = Images.selectedImage

Upvotes: 0

Gaurav Srivastava
Gaurav Srivastava

Reputation: 514

Subclass all 10+ viewcontroller(s) with a Parent ViewController.

@interface ViewController : ParentViewController

Call a method from viewDidLoad of ParentViewController.

// ParentViewController

-(void)viewDidLoad {

[self createBackgroundView];

}

Inside createBackgroundView, programmatically create UIImageView, and populate it using a public variable (as per your requirement).

Inside your ViewController's viewDidLoad, call [super viewDidLoad];

// One of your 10+ ViewController(s)

-(void)viewDidLoad {

[super viewDidLoad];

// rest of your code

}

Upvotes: 1

Related Questions