1

これは初心者の質問に聞こえるかもしれませんが、私は iOS 開発に不慣れです。

iPad ビューに UIWebView と UITableView があります。で、このshouldAutorotateToInterfaceOrientationように見栄えがするようにサイズを変更します。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {    
        if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTextView.frame = f;
            f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
            self.mTableView.frame = f;
        }
        if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
            CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTextView.frame = f;
            f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
            self.mTableView.frame = f;        
        }   
        return YES;
    }

質問: 最初の読み込みでは、テーブル ビューは正しいサイズで描画されますが、ポートレート モードに切り替えてからランドスケープ モードに戻ると、UITableView はフレームで指定された幅よりも広くなります。では、なぜこれが起こり、どのように修正するのでしょうか?

PS。同じ方法ですべてのセルのフレームを設定しようとしましたが、役に立ちませんでした。

PSS。これはデバイスでのみ発生し、シミュレーターでは UITableView が正しく表示されます

4

6 に答える 6

4

shouldAutorotateToInterfaceOrientation:は、View Controller が指定された向きをサポートしているかどうかを示すブール値のみを返します。このメソッドで UI 変換を行うべきではなく、代わりに ですべてを行いますwillAnimateRotationToInterfaceOrientation:duration:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{    
    if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
        self.mTextView.frame = f;
        f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
        self.mTableView.frame = f;
    }
    if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {     
        CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
        self.mTextView.frame = f;
        f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
        self.mTableView.frame = f;        
    }
}

UIViewController のドキュメントを見てください。よく説明されています。

于 2011-12-01T09:33:51.200 に答える
2

iOS はこの関数を 1 回だけ呼び出します。この作業を期待どおりに行うには、次の行を(void)viewDidLoad関数に追加する必要があります。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

そして(void)orientationChanged:(id)object機能を作る:

- (void)orientationChanged:(id)object{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        // some works
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        // another works
    }
}

お役に立てば幸いです!

于 2011-12-01T09:35:54.920 に答える
2

縦向きと横向きの両方のモードで UITableView のサイズを変更できます。UITableView は、以前の方向モードに対する比率でサイズを自動的に変更します。したがって、両方のモードで希望のサイズを修正できます。

これがうまくいくことを願っています。

于 2013-04-02T05:07:24.723 に答える
0

AutoResizingフレームのコーディングを行う必要はありませ ん

于 2013-04-02T05:53:24.820 に答える
0

最も簡単な方法は、ポートレート モードとランドスケープ モードで 2 つの異なるビューを作成できるため、モードが変わるたびにビューを簡単に変更できます。このような:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        self.view = self.portraitView;
    else 
        self.view = self.landscapeView;
    [tblScore reloadData];
    return YES;
}
于 2011-12-01T09:47:46.460 に答える
0

autoResizingビューのプロパティを確認してください。

于 2011-12-01T09:35:48.143 に答える