Unable to change background color on UICollectionView (iOS)

daydr3am3r picture daydr3am3r · Jul 22, 2014 · Viewed 21.6k times · Source

I'm writing my first iOS app and I seam to be having some problems in changing the backgroundColor of a UICollectionView.

The app has a navigation controller and certain views. Initially, I was able to change the color in my AppDelegate implementation file:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];

        feedsList = [[FeedsListTVC alloc] init];

        rootNC = [[RootNC alloc] init];
        rootNC.viewControllers = [NSMutableArray arrayWithObjects:feedsList, nil];

        self.window.rootViewController = rootNC;

        return YES;
    }

Using

    self.window.backgroundColor = [UIColor whiteColor];

I was able to change the background color on all my views. However, I decided to add one more view (the UICollectionView) and to set it as the main view. So I changed the AppDelegate to this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];

        CollectionViewLayoutCVL *collectionViewLayout = [[CollectionViewLayoutCVL alloc] init];
        self.viewController = [[MainViewControllerCVC alloc] initWithCollectionViewLayout:collectionViewLayout];

        rootNC = [[RootNC alloc] init];
        rootNC.viewControllers = [NSMutableArray arrayWithObjects:viewController, nil];

        self.window.rootViewController = rootNC;

        return YES;
    }

From my point of view this looks mostly the same.

I also tried this inside the implementation file of the UICollectionView but this changes the color of the view controller not the color of the Collection's view as far as I can tell:

    self.view.backgroundColor = [UIColor whiteColor]; 

Any ideas?

Answer

Saheb Roy picture Saheb Roy · Jul 22, 2014

self.collectionView.backgroundColor = [UIColor yourColor];

Hope this helps