Center a UIView vertically and horizontally within its parent view

I have a UICollection view that is inside another UIView. I am trying to center the collection view inside its parent view. Currently manually doing it by tweaking its frame (see code below). There has to be a easier way.


self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(COLLECTION_PADDING+10, COLLECTION_PADDING+5, self.bounds.size.width - (COLLECTION_PADDING*2), self.bounds.size.height - (COLLECTION_PADDING+22)) collectionViewLayout:flow];


You could do something like this: = CGPointMake(CGRectGetMidX(superview.bounds), CGRectGetMidY(superview.bounds));

Swift 3: = CGPoint(superview.bounds.midX, superview.bounds.midY)

With this approach, your subview will be centered in its superview regardless of the superview's coordinates. Using an approach like this: =

Will cause problems if the superview has an origin of anything other than 0,0

You can setup your collectionView's width and height and then use the center property to adjust it's location.

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,width,height)]; = CGPointMake(,;

EDIT: As Logan just mentioned in the comments, this only works if self and self.collectionView have the same superview. If you are adding collectionView to a view and want it centered inside that view then you could do:

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,width,height)]; = CGPointMake( - self.frame.origin.x/2, - self.frame.origin.x/2);

But this case doesn't work when there is rotation involved. In that case you would have to use other means. So, as anything in coding, this is not a clear cut answer. But for most cases it will work. If you are getting into the rare cases that it does not work then you are likely going to do more custom framing than just centering inside parents.

Heard of Layouts?..This might help you.

This is for horizontal alignment

NSLayoutConstraint *centerHcons =[NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:collectionViewSuper attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];

This is for vertical alignment

NSLayoutConstraint *centerVcons =[NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:collectionViewSuper attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

    [collectionViewSuper addConstraints:@[centerHcons, centerVcons]];

IMO, you could try finding the difference between your collectionview and your parent view size. Then dividing by 2 and padding all sides with that.

parentHeight = self.bounds.size.height;
parentWidth = self.bounds.size.width;
heightDiff = parentHeight - collectionview.bounds.size.height;
widthDiff = parentWidth - collectionview.bounds.size.width;
heightPadding = heightDiff/2;
widthPadding = widthDiff/2;

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(widthPadding, heightPadding, parentWidth-widthPadding, parentHeight-heightPadding) collectionViewLayout:flow];

Need Your Help

Transfer data between iOS and Android via Bluetooth?

android ios bluetooth gamekit bonjour

I've been reading up on how to transfer data between iOS devices over Bluetooth using GameKit. I'm not writing a game, per se, but do have a need to transfer a small amount of binary data between ...

Android device is not connected to USB for debugging (Android studio)

android android-studio debugging adb

I am trying to debug my application using a real device instead on the emulator. When I try to connect, it gives me the message: