これは、現在の日付を見ると少し古い質問かもしれませんが、ごく最近、同じ問題に直面しました。メイン ビューのサブビューやそのレイヤーの変換など、多くの提案に出くわす可能性があります。これのどれも私にとってはうまくいきませんでした。
実際に私が見つけた唯一の解決策は、UI コントロールを動的に配置したいので、主にインターフェイス ビルダーに配置しないことです。インターフェイス ビルダーは、縦向きと横向きの両方で動的コントロールの目的の場所を知るのに役立ちます。つまり、Interface Builder で 2 つの別々のテスト ビュー (1 つは縦向き、もう 1 つは横向き) を作成し、コントロールを必要に応じて配置し、X、Y、幅、高さのデータを右下に配置して、各コントロールの CGRectMake で使用します。
インターフェイスビルダーから必要なすべてのポジショニングデータを書き留めたらすぐに、それらの既に描画されたコントロールとアウトレット/アクションリンクを取り除きます. それらは今では必要ありません。
もちろん、UIViewController の willRotateToInterfaceOrientation を実装して、方向が変わるたびにコントロールのフレームを設定することを忘れないでください。
@interface
//Declare your UI control as a property of class.
@property (strong, nonatomic) UITableView *myTable;
@end
@implementation
// Synthesise it
@synthesize myTable
- (void)viewDidLoad
{
[super viewDidLoad];
// Check to init for current orientation, don't use [UIDevice currentDevice].orientation
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 228, 312)];
}
else if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
myTable = [[UITableView alloc] initWithFrame:CGRectMake(78, 801, 307, 183)];
}
}
myTable.delegate = self;
myTable.dataSource = self;
[self.view addSubview:myTable];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
// Show landscape
myTable.frame = CGRectMake(20, 20, 228, 312);
}
else
{
// Show portrait
myTable.frame = CGRectMake(78, 801, 307, 183);
}
}