How to Define the Size of a Collectionview on Rotate

How do I define the size of a CollectionView on rotate

Heres my 2 cents - because your item sizes are static why don't you set the item size on the collectionViewLayout?

It will be quicker to do this rather than expecting the collection view to call its delegate method for every cell.

viewWillLayoutSubviews can be used for detection of rotation on view controllers.
invalidateLayout can be used on a collection view layout to force it to prepare the layout again causing it to position the elements with the new sizes in this case.

- (void)viewWillLayoutSubviews;
{
[super viewWillLayoutSubviews];
UICollectionViewFlowLayout *flowLayout = (id)self.firstCollectionView.collectionViewLayout;

if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation)) {
flowLayout.itemSize = CGSizeMake(170.f, 170.f);
} else {
flowLayout.itemSize = CGSizeMake(192.f, 192.f);
}

[flowLayout invalidateLayout]; //force the elements to get laid out again with the new size
}

edit: updated with Swift2.0 example

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}

if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
flowLayout.itemSize = CGSize(width: 170, height: 170)
} else {
flowLayout.itemSize = CGSize(width: 192, height: 192)
}

flowLayout.invalidateLayout()
}

UICollectionView - resizing cells on device rotate - Swift

You might use viewWillLayoutSubviews. This question should be helpful but this is bassically called whenever the view controller views is about to layout its subviews.

So your code will look like this:

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}

if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
//here you can do the logic for the cell size if phone is in landscape
} else {
//logic if not landscape
}

flowLayout.invalidateLayout()
}

Update UICollectionView Width on Device Rotate in Swift

You need to implement the UICollectionViewDelegateFlowLayout protocol on your view controller.

On viewDidLoad, register for orientation changed notifications:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationDidChange:", name: UIDeviceOrientationDidChangeNotification, object: nil)

Don't forget to remove the observer when the viewController is closed:

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

When the orientation changes, tell the collectionView to update the layout:

func orientationDidChange(notification: NSNotification) {
collectionView!.collectionViewLayout.invalidateLayout()
}

Finally, implement the UICollectionViewDelegateFlowLayout protocol method. On this method you can calculate the size for your collection view cells as needed, by referencing the collectionView frame width or the device screen size:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// Leave 10px margin on each side, plus 10px between columns
let columns = 2
let margings = 20 + 10 * (columns - 1)
let width = (collectionView.frame.size.width - margings) / columns
let height = width // square cells
return CGSizeMake(width, height)
}

In my case, I want the cells to always be proportional to the portrait screen ratio. Also I have a variable number of columns, but that might not be your case.

How to change UiCollectionView cell size when device orientation changes?

As @krishnanunni already mentioned,

Please check whether shareLogCollectionView has the right outlet connected.
This is a very common mistake.

also for rotate you can use viewWillTransitionToSize

Please check.

Swift: How to refresh UICollectionView layout after rotation of the device

Add this function:

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
myCollection.collectionViewLayout.invalidateLayout()
}

When you change the orientation, this function would be called.



Related Topics



Leave a reply



Submit