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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24888915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:30:00  来源:igfitidea点击:

Unable to change background color on UICollectionView (iOS)

iosuicollectionviewuicollectionviewlayoutuibackgroundcolor

提问by daydr3am3r

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

我正在编写我的第一个 iOS 应用程序,但我在更改 UICollectionView 的背景颜色时遇到了一些问题。

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. So I changed the AppDelegate to this:

我能够更改所有视图的背景颜色。但是,我决定再添加一个视图(UICollectionView)并将其设置为主视图。所以我把 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?

有任何想法吗?

回答by Saheb Roy

self.collectionView.backgroundColor = [UIColor yourColor];

self.collectionView.backgroundColor = [UIColor yourColor];

Hope this helps

希望这可以帮助

回答by Desert Rose

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