简体   繁体   English

无法更改 UICollectionView (iOS) 上的背景颜色

[英]Unable to change background color on UICollectionView (iOS)

I'm writing my first iOS app and I seam to be having some problems in changing the backgroundColor of a UICollectionView.我正在编写我的第一个 iOS 应用程序,并且在更改 UICollectionView 的 backgroundColor 时遇到了一些问题。

The app has a navigation controller and certain views.该应用程序具有导航控制器和某些视图。 Initially, I was able to change the color in my AppDelegate implementation file:最初,我能够更改 AppDelegate 实现文件中的颜色:

    - (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.但是,我决定再添加一个视图(UICollectionView)并将其设置为主视图。 So I changed the AppDelegate to this:所以我将 AppDelegate 更改为:

    - (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:我也在 UICollectionView 的实现文件中尝试了这个,但是据我所知,这改变了视图控制器的颜色,而不是集合视图的颜色:

    self.view.backgroundColor = [UIColor whiteColor]; 

Any ideas?有任何想法吗?

self.collectionView.backgroundColor = [UIColor yourColor];

希望这可以帮助

UIView *redView = [UIView new];
redView.backgroundColor = [UIColor redColor];
_collectionView.backgroundView = redView;

Objective-C 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM