我必须绘制一个带有部分的菜单,该部分必须支持仅在iPad上旋转。
当我第一次在设备上启动应用程序时,即使设备处于纵向或横向模式,一切都很好。
旋转后,我收到包含单元格的视图的错误大小。
-(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++;
}
}
我在viewwillappear
中以及用户在didRotateFromInterfaceOrientation
中旋转设备后调用此方法。
我在xib中添加了带有self.viewMenuArea的printscreen。
关于视图的正确框架的任何建议,其中包含单元格(self.viewMenuArea
)?
答案 0 :(得分:1)
您可能已经将这些枚举混合为UIDeviceOrientation和UIInterfaceOrientation。 首先,尝试改变
if((interfaceOrientationValue == UIDeviceOrientationLandscapeRight) || (interfaceOrientationValue == UIDeviceOrientationLandscapeLeft)){
到
if(UIInterfaceOrientationIsLandscape(interfaceOrientationValue)) {
您还应该注意XIB /故事板中设置的自动调整遮罩/自动布局约束,它可能会影响viewMenuArea
的框架。只是为了确保那些不会影响你的viewMenuArea,在XIB / storyboard中从该视图中删除所有这些,然后再试一次。视图的框架不应该改变。
除此之外,将视图与viewController分开并在其`layoutSubviews&#39;中实现所有布局内容是一个很好的做法。方法