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
}

ご協力いただきありがとうございます!

4

2 に答える 2

0

これを見てください: iphone/ipad の向きの処理

回転に応じて各コントロールの位置を指定するだけです。

于 2011-07-08T13:51:05.827 に答える
0

これは、現在の日付を見ると少し古い質問かもしれませんが、ごく最近、同じ問題に直面しました。メイン ビューのサブビューやそのレイヤーの変換など、多くの提案に出くわす可能性があります。これのどれも私にとってはうまくいきませんでした。

実際に私が見つけた唯一の解決策は、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);
    }
}
于 2014-04-08T08:38:39.050 に答える