6

I've got a TabBarController which has two tabs. In each tab I've got UICollectionView and UITapGestureRecognizer which fires every time when I tap on collectionView. Everything works fine after the app is started. But if I rotate to landscape orientation, TapGestureRecognizer fires only in the collectionView's old frame. It absolutely ignores the right side of the screen.

But if I switch to another tab and then back, it'll work fine for landscape orientation too. I just don't understand what I'm doing wrong.

That's how I change orientation mode for collectionView:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self.cardCollectionView.collectionViewLayout invalidateLayout];
}
Appulus
  • 18,630
  • 11
  • 38
  • 46
Konstantin
  • 861
  • 4
  • 12
  • check the size of parent view after rotation. – Muruganandham K Nov 18 '13 at 11:22
  • size of parent view is correct after rotation and still the recogniser works only in frame which was before rotation. And it starts working properly right after I switch to another tab and come back. – Konstantin Nov 18 '13 at 15:34
  • I have a same problem. After rotation, TapGestureRecognizer does not work if the device is portrait orientation. it works if the device is landscape orientation. – KNaito Mar 24 '15 at 10:36

1 Answers1

0

invalidateLayout doesn't actually invalidate layout of collection view itself, it force invalidation and recalculating of internal cell layout - doc

To handle collection view frame changes you have to think about it like about regular view. Try to set autoresizing mask of your collection view (for example in viewDidLoad) like:

self.cardCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

UPDATE

try to set autoresizing for your collection view to http://d.pr/i/nk3o, by code it will looks like:

self.cardCollectionView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|

;

UIViewAutoresizingFlexibleBottomMargin is important to say UIKit from where calculate position

in.disee
  • 1,102
  • 1
  • 7
  • 15
  • After I change orientation, I see that collectionViewCelles has been drown fine and filled all collectionView, but tapRecognizer does't fire. I've tried your solution, but it's doesn't work either. – Konstantin Nov 18 '13 at 12:32
  • try to set backgroundColor of your collectionView to something like [UIColor redColor] and see what is actual frame of collection view - is it correct after rotation? – in.disee Nov 18 '13 at 13:07
  • I did it and size of collection view is correct, but still have the problem with firing tapGesture – Konstantin Nov 18 '13 at 13:48
  • how do you add gesture? do you remove invalidateLayout? – in.disee Nov 18 '13 at 14:32
  • I add gesture though interface. Now, properties of UITapGestureRecognizer looks like [this](http://ge.tt/1ldrW301/v/0?c) and also I cleared my method `willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration` still rotates fine, but recogniser doesn't work right after launch, only after I change tabs. – Konstantin Nov 18 '13 at 14:47
  • try to add gesture in code, like UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flipCard:)]; [self.cardCollectionView addGestureRecognizer:tap]; – in.disee Nov 18 '13 at 16:27
  • Added that code in viewDidLoad. Unfortunately, it doesn't help. Still have the same effect. Tap recognises only in old frame's after rotation and works right right after I change tabs and get back to previous. – Konstantin Nov 19 '13 at 10:36