25

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?

daydr3am3r
  • 920
  • 4
  • 12
  • 31

3 Answers3

48

self.collectionView.backgroundColor = [UIColor yourColor];

Hope this helps

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • Wow, that was easy. I can't believe I missed this. Thanks. – daydr3am3r Jul 22 '14 at 14:07
  • 1
    Does anyone know why this works but self.view.backgroundColor does not? I'd expect view and collection view to refer to the same instance. – Greg Brown Oct 13 '15 at 15:37
  • 1
    self.view.backgroundColor works too, but if i take a wild guess why u cant see it , it is probably coz the collectionview is over the self.view. and the collectionview's color is opague white or smthing like that. – Saheb Roy Oct 14 '15 at 08:19
3
UIView *redView = [UIView new];
redView.backgroundColor = [UIColor redColor];
_collectionView.backgroundView = redView;
Desert Rose
  • 3,376
  • 1
  • 30
  • 36
1

Objective-C

No Color

self.collectionView.backgroundColor = [UIColor clearColor];

Any color

self.collectionView.backgroundColor = [UIColor redColor];    
self.collectionView.backgroundColor = [UIColor blueColor];    
self.collectionView.backgroundColor = [UIColor yourColor];

Swift

No Color

self.collectionView.backgroundColor = UIColor.clear

Any color

self.collectionView.backgroundColor = UIColor.red
self.collectionView.backgroundColor = UIColor.blue
self.collectionView.backgroundColor = UIColor.yourColor
Enrique
  • 1,586
  • 17
  • 14