2011-07-08 15 views
1

私はiPadのアプリを持っています。プログラム、画像、ラベル、ボタンを私のビューに配置し、CGRectMake(x、x、x、x)を使用してビューを中心にどこに移動するかを指示しました。アプリケーションが水平に回転すると、ラベルやボタンが上に移動する必要があります(横向きモードでは下に移動できないため)が、中央に留まります。あなたの助けのためのビューが回転しているときにオブジェクトを移動する

if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)) 
{ 
    lblDate = [[UILabel alloc] initWithFrame:CGRectMake(384-(fieldWidth/2)-30,controlTop+45,120,40)]; //these dimensions aren't correct, though they don't matter here 

    lblDate.text = @"Date:"; 
    lblDate.backgroundColor = [UIColor clearColor]; 
    [contentView addSubview:lblDate]; 
} else { 
    //the orientation must be portrait or portrait upside down, so put duplicate the above code and change the pixel dimensions 
} 

ありがとう:ここで私がプレーしてきたいくつかのコードです!

答えて

0

これは現在、日付を見ている古い質問のビットかもしれませんが、私はまもなく同じ問題に直面しました。メインビューのサブビューやそのレイヤーを変換するなど、多くの提案があります。これのうちのどれも私にとっては役に立たなかった。

私が見つけた唯一の解決策は、UIコントロールを動的に配置したいから、主にインターフェイスビルダーに配置しないことです。インタフェースビルダーは、ポートレートとランドスケープの両方の方向でダイナミックコントロールの望ましい場所を知るのに役立ちます。つまり、インターフェイスビルダーで2つの別々のテストビューを作成し、1つのポートレートと他のランドスケープを作成し、それぞれのコントロールに対してCGRectMakeで使用するX、Y、WidthおよびHeightデータを右下に配置します。

インターフェイスビルダから必要なすべての測位データを書き留めるとすぐに、既に描画されたコントロールとアウトレット/アクションリンクを取り除きます。彼らは今必要ではありません。

もちろん、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); 
    } 
} 
関連する問題