-
Notifications
You must be signed in to change notification settings - Fork 1
I 5.1. Constructing Collection Views
Either use an instance of the UICollectionView as a subview of another view in your app or (if you want a full-screen collection view) use the UICollectionViewControl ler class.
Let’s explore the full-screen scenario first. Follow these steps to create a simple app that displays a full-screen collection view on the screen: Open Xcode and create Empty Application. Now that you have your project set up, create a new class in your project and call it ViewController. This class has to subclass UICollectionViewController. You won’t need a .xib file for this view controller, so skip that option. Find and open the AppDelegate.m file in your project (your app delegate’s imple‐ mentation file), create an instance of your collection view, and make that collection view the root view controller of your app, as shown here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* Instantiate the collection view controller with a valid flow layout */ ViewController *viewController = [[ViewController alloc] initWithCollectionViewLayout:[self flowLayout]]; self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; /* Set the collection view as the root view controller of our window */ self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; return YES; }
If you run your app, it will crash with an exception complaining that you have provided a nil layout object to your collection view, and the runtime is right. You cannot do this. But I have not yet covered how to instantiate layout objects and pass them to the collection view, so for now, this is the best we can do. You will learn more about collection view layout objects later in this chapter.