0

これらの両方のコード行の違いは何ですか?

    self.view.frame = CGRectMake(0, 0, 320, 480);
    self.view.superview.frame = CGRectMake(0, 0, 800, 900);

ラベルの位置が変わるため、向きが変わるときにビューフレームを変更したいのですが、ラベルを画面の中央に配置したいのですが、誰かが私を案内してくれますか?

オリエンテーションのために次のデリゲートメソッドを使用していますが、動作していません

self.view.frame

しかし、次の行で問題なく動作しています

self.view.superview.frame

次のコードを参照してください

// Override to allow orientations other than the default portrait orientation.
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation     {

// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    NSLog(@"LEFT");
    self.view.frame = CGRectMake(100, 0, 480, 320);
    NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
   // self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
    [button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    NSLog(@"RIGHT");
    self.view.frame = CGRectMake(0, 0, 320, 480);
    NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
         //self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if           I will uncomment it
    [button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    self.view.frame = CGRectMake(0, 0, 320, 480);
    //self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if           I will uncomment it
    [button setFrame:CGRectMake(250, 400, 100, 30)];    
}
return YES;
}
4

2 に答える 2

1

self.viewself のビューです (viewController について話している場合)。 self.view.superview保持しているビューですself.view

つまり、ウィンドウにビューを追加すると、そのビューのスーパービューがウィンドウになります。

自動サイズ変更マスクが正しく設定されていない場合、フレームの設定は失敗します。

于 2011-09-12T09:48:11.110 に答える
0

一般的なステートメントとして、最初の行は、このコードが記述されている現在の viewController のビューのフレームを設定しようとしています。

2 行目は、現在の viewController のビューのビューの親ビューのフレームを設定しようとしています。

これが正確に何を意味し、どれを使用する必要があるかは、アプリケーションで設定したビュー階層によって異なります。

于 2011-09-12T09:50:31.997 に答える