我有一个 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];
}
}
}
}