Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Photo Browser iPhone App UIKit Dynamics Extra Credit

Custom subclass of UICollectionView doesn't properly initialize

As part of the Photo Bomber app course, there is a custom initialization via the UICollectionViewFlowLayout class of the custom subclass of UICollectionView as follows:

- (instancetype)init {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(106.0,106.0);
    layout.minimumInteritemSpacing = 1.0;
    layout.minimumLineSpacing = 1.0;

    return (self = [super initWithCollectionViewLayout:layout]);
}

This was working perfectly well, displaying 3 photos of dimension 106 x 106 per row in my custom subclass of UICollectionView, when I had implemented the following method in my AppDelegate.m:

- (void) customizeUI {
    Customize the nav bar

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBarBackground"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:255.0 / 255.0 green:160.0 / 255.0 blue:40.0 / 255.0 alpha:1.0]}];

   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

THFriendsViewController *photosFriendsViewController = [[THFriendsViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:photosFriendsViewController];


 UINavigationBar *navigationBar = navigationController.navigationBar;
  navigationBar.barTintColor = [UIColor colorWithRed:255.0 / 255.0 green:160.0 / 255.0 blue:40.0 / 255.0 alpha:1.0];
   navigationBar.barStyle = UIBarStyleBlackOpaque;

Set THFriendsViewController as rootViewController may need to revert back to THPhotosViewController
   self.window.rootViewController = navigationController;

   self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


}

After an interesting discussion with Thomas on the treehouse forum ( see: https://teamtreehouse.com/forum/performseguewithidentifier-crash ), I decided to comment out the customizeUI method from my code and define the storyboard as my main interface for this project.

This solved an issue I had with a segue between two custom subclasses of UICollectionViewController but caused the UICollectionViewFlowLayout to no longer initialize properly.

Instead of displaying 3 images of 106 x 106 spaced out by 1 point, I now get 5 much smaller images per row spaced out by at least 5 points.

How come?

What should I do to ensure the custom layout I had designed keeps on being displayed?

Merci!

3 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

Hello again!

You can use different approaches to accomplish that. The easiest way in this case would be to use the storyboard (or, you could override one of the UICollectionViewDelegateFlowLayout method's if you'd like).

http://imgur.com/iG2Wv9b

Hello again Thomas!

At least I can tell that you are active in our community. Thanks again :)

The trick was to make sure to size the cell in the IB at the CollectionViewController level and not the CollectionViewCell level this the former seems to override the latter...

It worked perfect. Thanks so much.

Where should I send the Champagne?

;)