2

デバイスの回転時に適切に配置する必要があるcontentView3つの UI 要素があります。UITableViewCell現在、これらの UI 要素のフレームを次のように適用しています。

segmentedControl1.frame = CGRectMake(165, 15, 130, 40);
segmentedControl2.frame = CGRectMake(435, 15, 130, 40);
segmentedControl3.frame = CGRectMake(710, 15, 130, 40);

これらの要素の autoresizingMask プロパティを使用して、デバイスが横向きから縦向き (デフォルトは縦向き、iPad のみ) に回転したときに、これらの要素のサイズを変更して互いに重ならないようにする方法を知りたいです。向きごとにカスタムのフレーム位置を設定したくありません。

私はこのようなことを試しました:

segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;

しかし、これはうまくいかないようです。要素はすべて重なり合っており、不幸にもレイアウトされています。

助言がありますか?

更新 1

テーブル ビュー セルは現在、ランドスケープ モードでどのように表示されるかを次に示します。

--------------------------------------------------------------------------
|    ============              ============             ============     |
|   | A   |  B   |            | A   |  B   |           | A   |  B   |    |
|    ============              ============             ============     |
--------------------------------------------------------------------------

そして、デバイスが縦向きモードに回転したときに私が望むものは次のとおりです。

-------------------------------------------------------
|    ============    ============    ============     |
|   | A   |  B   |  | A   |  B   |  | A   |  B   |    |
|    ============    ============    ============     |
-------------------------------------------------------

UPDATE 2 @Wolfert からの提案を組み込んだ cellForRowAtIndexPath の私のコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // Configure the cell...
    segmentedControl1.frame = CGRectMake(165, 15, 180, 40);
    segmentedControl1.tag = indexPath.row;
    segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    [cell.contentView addSubview:segmentedControl1];

    segmentedControl2.frame = CGRectMake(435, 15, 180, 40);
    segmentedControl2.tag = indexPath.row;
    segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [cell.contentView addSubview:segmentedControl2];

    segmentedControl3.frame = CGRectMake(710, 15, 180, 40);
    segmentedControl3.tag = indexPath.row;
    segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [cell.contentView addSubview:segmentedControl3];

    return cell;
}
4

2 に答える 2

3

これでうまくいくはずです:

segmentedControl1.autoresizingMask =   UIViewAutoresizingFlexibleRightMargin;
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

スーパービューには次のプロパティが必要であることに注意してください。

view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
于 2011-12-19T10:42:54.547 に答える
1

ランドスケープ構成が機能しているとのことで、少し混乱しています。CGRectMakeハードコードされた数値と引用したautoresizingMask値を組み合わせると、すでに歪んだレイアウトになると私は言ったでしょう。

とはいえ、ここに私のアドバイスがあります。最初のフレームの幅にハードコーディングされた数値を使用しないでください。代わりに、 に基づいて計算する必要がありますcell.contentView.bounds.size.width。あなたの場合、次のようなものです:

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
  static NSString* cellIdentifier = @"Cell";
  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

  NSArray* items = [NSArray arrayWithObjects:@"A", @"B", nil];
  UISegmentedControl* segmentedControl1 = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
  UISegmentedControl* segmentedControl2 = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
  UISegmentedControl* segmentedControl3 = [[[UISegmentedControl alloc] initWithItems:items] autorelease];
  [cell.contentView addSubview:segmentedControl1];
  [cell.contentView addSubview:segmentedControl2];
  [cell.contentView addSubview:segmentedControl3];

  // Some values I like
  CGFloat marginHorizontal = 10;
  CGFloat spacingHorizontal = 8;
  // This is the crucial line!
  CGFloat totalWidth = cell.contentView.bounds.size.width;
  // That's how much is available to the three controls 
  CGFloat availableWidth = totalWidth - 2 * marginHorizontal - 2 * spacingHorizontal;
  // That's how much each control gets - initially!
  CGFloat segmentedControlWidth = availableWidth / 3.0;

  // These are the numbers from your example. With these numbers your table view
  // delegate probably implements tableView:heightForRowAtIndexPath:()
  CGFloat marginVertical = 15;
  CGFloat segmentedControlHeight = 40;

  CGRect segmentedControlFrame = CGRectMake(marginHorizontal,
                                            marginVertical,
                                            segmentedControlWidth,
                                            segmentedControlHeight);
  segmentedControl1.frame = segmentedControlFrame;
  segmentedControlFrame.origin.x += spacingHorizontal + segmentedControlWidth;
  segmentedControl2.frame = segmentedControlFrame;
  segmentedControlFrame.origin.x += spacingHorizontal + segmentedControlWidth;
  segmentedControl3.frame = segmentedControlFrame;

  // These are the values you cited initially, they are fine
  segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
  segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;

  return cell;
}

iPadシミュレーターでこれを試してみましたが、うまくいきました。autoresizingMaskセルまたはコンテンツ ビューのを変更する必要はありません。これが通常の動作とは異なることは知っていUIViewますが、テーブルビューのセルが奇妙な獣であることを除いて説明がありません:-)

元のコードに戻る: 根本的な問題は、ハードコードされた数値が、コンテンツ ビューが画面の全幅であると想定しているのに、実際には最初の幅がはるかに小さい (私の環境でcell.contentView.bounds.size.widthは最初は 320 である) ことです。したがって、初期レイアウトは次のようになります。

+-content view------------+
|    ============         |    ============             ============ 
|   | A   |  B   |        |   | A   |  B   |           | A   |  B   |
|    ============         |    ============             ============ 
+-------------------------+

ご覧のとおり、レイアウトはコンテンツ ビューの初期幅に比例していません。コンテンツ ビューが後で正しい幅にサイズ変更されると、コントロールも最初の幅と水平方向の分布に比例してサイズ変更されます。これにより、さらに不均衡が生じます:-)

于 2012-01-24T23:44:21.813 に答える