0

UITableView のヘッダ部に UIButton を表示したい。これは私が試したコードです

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 285, 44)];

    UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 320.0, 30.0);
    headerViewButton.backgroundColor = [UIColor grayColor];
    [headerViewButton setTitle:@"Import main list from other field  >" forState:UIControlStateNormal];
    [headerViewButton addTarget:self
                          action:@selector(buttonPressed:)
                forControlEvents:UIControlEventTouchDown];

    [headerView addSubview:headerViewButton];

問題は、ポートレート モードだけでなくランドスケープ モードでもうまく収まるように、ボタン フレームを調整するのに苦労していることです。私は Interface Builder の使用にあまり慣れていません。ビューに合うようにボタン フレームを正しく調整するにはどうすればよいですか。

使ってみましたsetAutoresizingMask

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 290, 30.0);
    headerViewButton.backgroundColor = [UIColor grayColor];
    [headerViewButton setTitle:@"Import main list from other field  >" forState:UIControlStateNormal];
    [headerViewButton addTarget:self
                         action:@selector(buttonPressed:)
               forControlEvents:UIControlEventTouchDown];

    [headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];

    [headerView addSubview:headerViewButton];

アプリを初めてロードするとボタンが表示されなくなりますが、向きを変えるとボタンが表示されます。何度やっても同じパターン。アプリが初めて読み込まれると、ボタンが表示されなくなり、その後は両方の向きで表示されます。

4

2 に答える 2

0

設定してみる

[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

このようにして、スーパービューが変更されるとサイズが変更されます。(IB で自動サイズ変更オプションを設定するのと同じように機能します)

于 2013-03-18T16:33:53.707 に答える
0

autoresizingMaskボタンを適切な値に設定する必要があります。値は、親ビューのサイズの変更に合わせてボタンのサイズや位置を調整する方法によって異なります。

あなたが投稿したコードでは、ボタンを親ビューよりも広くしているのは奇妙です。

于 2013-03-18T16:34:06.427 に答える