I have to draw a menu with section which have to supports rotate only on iPad.
When I start application first time on device everything is fine even if the device is in portrait or landscape mode.
After the rotation I receive wrong size for the view wich contain the cells.
-(void)buildMenu:(UIInterfaceOrientation)interfaceOrientationValue{
CGFloat padding = 1;
CGFloat widthCell = 0;
CGFloat heightCell = 0;
int rowNo = 0;
int columnsNo = 0;
int goToNextRow = 0; // used when we draw the menu - start drawing cell to the next row in menu
if((interfaceOrientationValue == UIDeviceOrientationLandscapeRight) || (interfaceOrientationValue == UIDeviceOrientationLandscapeLeft)){//iPad Landscape - FH only for iPad support rotation
rowNo = kLandscapeColumns; // 2
columnsNo = kLandscapeRows; // 3
widthCell = (self.viewMenuArea.frame.size.width/columnsNo-rowNo*padding);
heightCell = (self.viewMenuArea.frame.size.height/rowNo-padding);
goToNextRow = columnsNo; // go to the next line/row when we were draq the cell 4.
}else{//iPad || iPhone - Portrait
rowNo = kPortraitRows; //2
columnsNo = kPortraitColumns; //3
widthCell = (self.viewMenuArea.frame.size.width/rowNo-padding);
heightCell = (self.viewMenuArea.frame.size.height/columnsNo-padding);
goToNextRow = rowNo; // go to the next line/row when we were draq the cell 3.
}
//Draw Menu Cells
CGFloat x = 0;
CGFloat y = 0;
int itemsCount = 1;
for(int i=0; i<self.dataSource.count; i++){
[FHMenuCell buildMenuCell_initWithTitle:[self.dataSource objectAtIndex:i] isDisabled:NO xOrigin:x yOrigin:y width:widthCell height:heightCell pendingItems:0 owner:self container:self.viewMenuArea delegate:self tag:i];
if (itemsCount % goToNextRow == 0){
y += heightCell + padding;
x = 0;
}else{
x += widthCell+padding;
}
itemsCount++;
}
}
I call this methods in viewwillappear
and after when user rotate the device in didRotateFromInterfaceOrientation
.
I added a printscreen with the self.viewMenuArea in xib.
Any advice regarding the correct frame of the view which contain the cells (self.viewMenuArea
)?
Possibly, you've mixed such enums as UIDeviceOrientation and UIInterfaceOrientation. First of all, try to change
if((interfaceOrientationValue == UIDeviceOrientationLandscapeRight) || (interfaceOrientationValue == UIDeviceOrientationLandscapeLeft)){
to
if(UIInterfaceOrientationIsLandscape(interfaceOrientationValue)) {
You should also pay attention to the autoresizing masks/ autolayout constraints set in your XIB/storyboard, it may affect the frame of your viewMenuArea
. Just to be sure that those won't affect your viewMenuArea, remove all of them from that view in XIB/storyboard and try again. the view's frame shouldn't change.
Other than that, it's a good practice to separate view from viewController and implement all the layout stuff in its `layoutSubviews' method.