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