【问题标题】:Incorrect width and height after rotation旋转后宽度和高度不正确
【发布时间】:2014-12-28 19:23:35
【问题描述】:

我必须绘制一个菜单,其部分必须支持仅在 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 的打印屏幕。

关于包含单元格的正确视图框架的任何建议 (self.viewMenuArea)?

【问题讨论】:

    标签: ios rotation resize frame screen-rotation


    【解决方案1】:

    您可能混合了诸如 UIDeviceOrientation 和 UIInterfaceOrientation 之类的枚举。 首先,尝试改变

        if((interfaceOrientationValue == UIDeviceOrientationLandscapeRight) || (interfaceOrientationValue == UIDeviceOrientationLandscapeLeft)){
    

        if(UIInterfaceOrientationIsLandscape(interfaceOrientationValue)) {
    

    您还应该注意在您的 XIB/故事板中设置的自动调整掩码/自动布局约束,它可能会影响您的 viewMenuArea 的框架。只是为了确保这些不会影响您的 viewMenuArea,请从 XIB/storyboard 中的该视图中删除所有这些,然后重试。视图的框架不应改变。

    除此之外,将视图与 viewController 分离并在其 `layoutSubviews' 方法中实现所有布局内容是一个很好的做法。

    【讨论】:

    • 我检查了 XIB 中的 viewMenuArea 并且没问题。我为 self.viewMenuArea 记录了框架,当我在 buildMenu 中以正确的方向旋转设备时,我有旧框架(例如,如果我是纵向的我将设备横向旋转,self.MenuArea 的框架与纵向方向相同)。如果我再次旋转框架,因为它是横向的正确框架......请提供任何建议?
    • 您提供的代码不会更改 self.viewMenuArea 的框架。因此,请尝试在您的项目中的某处寻找“viewMenuArea.frame =”,如果它没有被您的 xib 中引入的自动调整掩码或自动布局更改
    • 我通过将调用函数从 didRotateFromInterfaceOrientation 移动到 willAnimateRotationToInterfaceOrientation 来解决。
    • 感谢您的支持!
    猜你喜欢
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多