1

プログラムで UIDatePicker コントロールをビューに追加しています。標準的な方法で、DatePicker を画面の下部にドッキングして表示したい...

DatePicker のフレームを設定していますが、3.5 インチの iPhone と 4 インチの iPhone では画面サイズが異なることに注意する必要があります。

次のコードは目的の結果を生成していますが、いくつか質問があります...

// In ViewDidLoad
CGRect defaultFrame = CGRectMake(0,0,0,0);
_datePicker = [[UIDatePicker alloc] initWithFrame:defaultFrame];

CGRect bounds = [self.view bounds];
int datePickerHeight = _datePicker.bounds.size.height;
int navBarHeight = 44;
CGRect datePickerFrame = CGRectMake(0, bounds.size.height - (datePickerHeight + navBarHeight), 0, 0);
[_datePicker setFrame:datePickerFrame];

// In method responding to user tap
[self.view addSubview:_datePicker];

Q1. これを行うよりエレガントな方法はありますか?フレームでDatePickerを作成し、その高さを確認してからフレームを設定する以外の何か...

Q2. ビューは、UINavigationController 内にある UITableView です。self.view の境界を取得すると、サイズにはナビゲーションバーの 44 を含むビュー全体が含まれます。しかし、addSubview で DatePicker を追加すると、navBar のオフセットを含めないと、下から 44 ずれてしまいます...

[self.view bounds] が完全な境界を返すとき、addSubview が小さい境界内で動作するのはなぜですか?

乾杯、ギャビン

4

3 に答える 3

1

問題は、ビューのロードが初期化された後、ナビゲーションバーがすべてのビューを下に押してしまうことです。自動サイズ変更マスクが役立つ場合があります。

于 2013-02-06T13:08:55.327 に答える
1

これをもう少し調べたところ、元の質問に欠陥があることに気付きました。UIDatePicker をサブビューとして追加する場所が明確ではありませんでした。質問を更新しました。

私は今2つの答えを持っています:

1) ViewDidLoad に UIDatePicker を配置して追加します。ビュー サイズの変更に対処するには、Autoresizing を使用します。次に、ユーザーがコントロールをタップすると表示されるようにします。

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView = (UITableView*)self.view;
    _tableView.backgroundColor = [UIColor colorWithRed:0.941 green:0.941 blue:0.913 alpha:1.000];
    _tableView.backgroundView = nil;

    _datePicker = [[UIDatePicker alloc] init];

    CGRect bounds = [self.view bounds];
    int datePickerHeight = _datePicker.frame.size.height;
    _datePicker.frame = CGRectMake(0, bounds.size.height - (datePickerHeight), _datePicker.frame.size.width, _datePicker.frame.size.height);
    _datePicker.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

    _datePicker.isHidden = YES;
    [self.view addSubview:_datePicker];

    [_datePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
}

2) ViewDidLoad ではなく、必要に応じて UIDatePicker のフレームを設定するだけです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case RowDate:
        {
            CGRect bounds = [self.view bounds];
            int datePickerHeight = _datePicker.frame.size.height;
            _datePicker.frame = CGRectMake(0, bounds.size.height - (datePickerHeight), _datePicker.frame.size.width, _datePicker.frame.size.height);
            [self.view addSubview:_datePicker];
            break;
        }

        default:
            break;
    }
}

ありがとう、ギャビン

于 2013-02-07T13:05:03.580 に答える
0
  1. の場合UIDatePicker、サイズを指定する必要はありません。ほとんどの場合、画面と同じ幅が必要で、高さが固定されているためです。ただし、正しい位置に配置する必要があります。つまり、正しい位置を計算し、フレームを設定する必要があります。

  2. UIDatePickerほとんどの場合、をオーバーラップさせたくないからですnavBar。そのため、Apple はaddSubview境界が「小さい」かのように動作させます。

于 2013-02-06T09:40:01.383 に答える