0

我有一个 X 数量的按钮矩阵添加到视图控制器。在纵向模式下,它以完美的顺序显示按钮。但是,随着视图控制器的出现,我旋转到横向模式,按钮不会重新定位。但是,如果我旋转手机然后呈现视图控制器,它会重新定位横向模式的按钮。

我已经被这个问题困扰了一段时间,不知道为什么当视图控制器已经出现时按钮不会旋转。我希望有人可以在这里帮助我。

菲利普

这是首先创建按钮的代码:

- (void)createButtonMatrix {   
     for (int rows = 0; rows < 11; rows++) {
                    for (int columns = 0; columns < 6; columns++) {
                        NSUInteger chapter =  columns +  rows * 6 + 1;
                        if (chapter > 50)
                            break;
                        chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                        chapterButtons.frame = CGRectMake(columns * 54, rows * 38, 53, 37); // columns * X, rows * Y, width, height
                        chapterButtons.tag = chapter;
                        [chapterButtons setTitle:[NSString stringWithFormat:@"%d", chapterButtons.tag] forState:UIControlStateNormal];
                        [createButtonArray addObject:chapterButtons];
                        [viewController2.view addSubview:chapterButtons];
                    }
                }
    }

现在在旋转时重新定位它们:

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {

        if (orientation == UIInterfaceOrientationPortrait) {
              NSLog(@"Will rotate to portrait.");
            for (int rows = 0; rows < 11; rows++) {
                for (int columns = 0; columns < 6; columns++) {
                    NSUInteger chapter =  columns +  rows * 6 + 1;
                    if (chapter > 50)
                        break;
                    chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    chapterButtons.frame = CGRectMake(columns * 54, rows * 38, 53, 37); // columns * X, rows * Y, width, height
                    chapterButtons.tag = chapter;
                    [chapterButtons setTitle:[NSString stringWithFormat:@"%d", chapterButtons.tag] forState:UIControlStateNormal];
                }
            }
        }

        else if (orientation == UIInterfaceOrientationLandscapeRight ||orientation == UIInterfaceOrientationLandscapeLeft) {
                NSLog(@"Will rotate to landscape.");
                for (int rows = 0; rows < 7; rows++) {
                    for (int columns = 0; columns < 9; columns++) {
                        NSUInteger chapter =  columns +  rows * 9 + 1;
                        if (chapter > 50)
                            break;
                        chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                        chapterButtons.frame = CGRectMake(columns * 54, rows * 38, 53, 37); // columns * X, rows * Y, width, height
                        chapterButtons.tag = chapter;
                        [chapterButtons setTitle:[NSString stringWithFormat:@"%d", chapterButtons.tag] forState:UIControlStateNormal];
                    }
                }

        }
    }
4

3 回答 3

1

如果我很好地理解了您的代码,那么您正在重新创建方向更改按钮,但这些按钮永远不会添加到视图中。这是错误的。您应该只更改createButtonArray(已填充createButtonMatrix())的每个按钮的位置:不要在willAnimateRotationToInterfaceOrientation: 仅提取按钮createButtonArray并修改其frame属性的情况下创建按钮。

于 2013-06-27T17:19:54.987 回答
0

代码行

chapterButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];

创建 UIButton 类的新实例。您必须将该对象添加到视图中以使其可见。在您的代码中,您还创建了太多 UIButton 自动释放实例。

最好的办法是:

  1. 创建一个 NSArray 类型的实例变量,它将包含所有 UIButton 实例
  2. [UIButton new]通过或创建 UIButton 对象[[UIButton alloc] init]
  3. 在方法中将所有 UIButton 对象添加到该数组中- (void)createButtonMatrix
  4. 将所有 UIButton 对象添加到视图中
  5. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration通过我们之前创建的数组在方法中管理 UIButton 对象的帧
于 2013-06-27T11:08:44.033 回答
0

感谢 art-divin,感谢 Jean-Baptiste 通过电子邮件提供答案。下面是在旋转时重新定位按钮矩阵的代码。创建按钮矩阵的代码保持不变。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {

if (orientation == UIInterfaceOrientationPortrait) {
    COLUMNS = 7;
    ROWSS = 8;

    int i=0; // button count iterator
    for (int rows = 0; rows < ROWSS; rows++) {
        for (int columns = 0; columns < COLUMNS; columns++) {
            NSUInteger chapter =  columns +  rows * COLUMNS + 1;
            if (chapter > 50)
                break;
            UIButton *b = (UIButton *)[call.createButtonArray objectAtIndex:i++];
            b.frame = CGRectMake(columns * WIDTH, rows * 38, HEIGHT, 37); // columns * X, rows * Y, width, height
        }
    }

    [rotateController rotateChapterButtons_Portrait];
}

else if (orientation == UIInterfaceOrientationLandscapeRight ||orientation == UIInterfaceOrientationLandscapeLeft) {
    COLUMNS = 10;
    ROWSS = 9;
    int i=0; // button count iterator
    for (int rows = 0; rows < ROWSS; rows++) {
        for (int columns = 0; columns < COLUMNS; columns++) {
            NSUInteger chapter =  columns +  rows * COLUMNS + 1;
            if (chapter > 50) // > greater than
                break;
            NSLog(@"%i:",chapter);
            UIButton *b = (UIButton *)[call.createButtonArray objectAtIndex:i++];
            b.frame = CGRectMake(columns * 48, rows * 38, 47, 37); // columns * X, rows * Y, width, height
        }
    }

    [rotateController rotateChapterButtons_Landscape];
}

}

于 2013-07-01T06:16:25.507 回答