次のように xib ファイルからロードされるカスタム ビューを作成しました。
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
[[NSBundle mainBundle] loadNibNamed:@"TachoView" owner:self options:nil];
[self addSubview:self.view];
[self initUi];
}
return self;
}
デバイスの向きが変わると、一部のサブビューの自動サイズ変更と配置で問題が発生しています。これらのスクリーンショットは、動作を説明しています。
viewDidLoad の後、すべてが正常に見えます。
横向きモードに回転した後も、レイアウトは問題ありません。
しかし、protrait に戻すと、次のようになります。
同じファイル .h、.m、および .xib は、単一ビューのダミー プロジェクトでテストすると正常に動作します。したがって、xib での配置とサイズ変更の設定は問題ないと思います。私のアプリとの違いは、ビューがスクロールビューに配置され、他のいくつかのビューがあることです。向きを変更すると、これらすべてのビューのサイズが変更されるため、すべてが期待どおりにレイアウトされます。関連するコードは、最も内側の for ループです。
-(void) viewWillLayoutSubviews {
float h = self.view.frame.size.height;
float w = self.view.frame.size.width;
// set the size of the vertical scrollview
verticalView.frame = CGRectMake(0, 0, w, h * gridHeight);
verticalScrollView.frame = CGRectMake(0, 0, w, h);
[verticalScrollView setContentSize:CGSizeMake(w, h * gridHeight)];
[verticalScrollView layoutIfNeeded];
// set the size of the horizontal scrollviews
for (int i=0; i<verticalView.subviews.count; i++) {
UIScrollView *scrollView = (UIScrollView *) [verticalView.subviews objectAtIndex:i];
UIView *container = (UIView *) [[scrollView subviews] objectAtIndex:0];
int subviewCount = container.subviews.count;
scrollView.frame = CGRectMake(0, h*i, w, h);
[scrollView setContentSize:CGSizeMake(w * subviewCount, h)];
container.frame = CGRectMake(0, 0, w * subviewCount, h);
// set the size of the actual views
for (int j=0; j<container.subviews.count; j++) {
UIView *view = [container.subviews objectAtIndex:j];
// FIXME manchmal crashed hier die WebView. Muss das hier im
// ui thread ausgeführt werden? wird diese methode nicht vom
// ui thread aufgerufen?
view.frame = CGRectMake(w*j, 0, w, h);
}
}
}
2 回目のオリエンテーションの変更で何が問題になっているのでしょうか? ビュー全体を手動でレイアウトすることもできましたが、自動サイズ変更機能でも可能だと思います。