2016-07-09 11 views
0

UITableHeaderFooterViewにUIButtonを追加しようとしました。UITableViewControllerテーブルヘッダー自動レイアウト制約を使用してUIButtonを追加します。

特定のCGRectフレームを使用してコード内の位置とサイズを固定することができるようです(< - StackOverFlowで多くの検索を行いました)。そして、その位置は常に制御するのが難しく、常に飛行しています。

UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f); 
scanQRCodeButton.backgroundColor = [UIColor redColor]; 
[scanQRCodeButton setTitle:@"Hello" forState:UIControlStateNormal];  
[cell addSubview:scanQRCodeButton]; 

しかし、私が本当にしたいことは異なる画面サイズにそれを適応させるためにcenterX、centerYと比例幅などの「制約」を使用することです。そして、問題は一部 "constraintWithItems" が付属しています:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier"; 

    // Reuse the instance that was created in viewDidLoad, or make a new one if not enough. 
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier]; 
    if (headerView == nil) { 
     [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"]; 
     headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"TableViewSectionHeaderViewIdentifier"]; 
    } 

    headerView.textLabel.text = @"MISSION"; 
    headerView.contentView.backgroundColor = [UIColor blackColor]; 

上記と同じブロック内headerViewの上にUIButtonを作成します:

UIButton *goBack = [[UIButton alloc] init]; 
    [goBack setImage:[UIImage imageNamed:@"Back"] forState:UIControlStateNormal]; 
    [goBack addTarget:self 
      action:@selector(back:) 
forControlEvents:UIControlEventTouchUpInside]; 

    [headerView addSubview:goBack]; 

はのUITableViewからheaderViewを取得します次に、制約を設定し、headerViewを返します。

NSLayoutConstraint *constraint = [NSLayoutConstraint 
            constraintWithItem:headerView.goBack 
            attribute:NSLayoutAttributeWidth 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view 
            attribute:NSLayoutAttributeCenterX 
            multiplier:0.5 
            constant:0]; 
    [headerView addConstraint:constraint]; 
    return headerView; 

問題:私が "constraintWithItem"の "headerView.goBack"と言うと、コンパイラは "プロパティGoBackがタイプUITableHeaderFooterViewのオブジェクトに見つかりません"という警告を表示します。

アップデート1

Natarajanの答えから、私は今これを行うにしてみてください。

headerView.translatesAutoresizingMaskIntoConstraints = false; 
    goBack.translatesAutoresizingMaskIntoConstraints = false; 
    NSDictionary *metrics = @{ @"width"   : @(CGRectGetWidth(self.view.frame)/10) }; 
    [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[goBack]|" options:0 metrics:metrics views:@{@"goBack":goBack}]]; 
    [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[goBack]|" options:0 metrics:metrics views:@{@"goBack":goBack}]]; 

現在の問題: "メトリック" の部分は動作しません。私は固定数の代わりにheaderViewまたはtableViewに比例した幅を持たせたい。私は "self.view.frame"(self.tableView.frameも試しました)を使うべきですか?

アップデート2

今headerViewとの問題もあります。 "headerView.translatesAutoresizingMaskIntoConstraints = false;"を使用する場合、headerViewの位置も手動で設定する必要があります。ただし、次のコードでは、headerViewを正しく設定することはできません。私はそれを最初にtableViewに追加しようとし、UIButtonと同じ方法で設定しましたが、同じ方法では動作しませんでした。

[headerView addSubview:goBack]; 
[tableView addSubview:headerView]; 

headerView.translatesAutoresizingMaskIntoConstraints = false; 
goBack.translatesAutoresizingMaskIntoConstraints = false; 

[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[goBack(40.0)]|" options:0 metrics:nil views:@{@"goBack":goBack}]]; 
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[goBack(40.0)]|" options:0 metrics:nil views:@{@"goBack":goBack}]]; 
[tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerView]|" options:0 metrics:nil views:@{@"headerView":headerView}]]; 
[tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView]|" options:0 metrics:nil views:@{@"headerView":headerView}]]; 

答えて

0

「goBack」はheaderViewプロパティとしてアクセスできないと思います。それを見つけることを試みなさい。 UITableViewHeaderFooterViewはあなただけUITableViewHeaderFooterViewのサブビューとして追加した「GOBACK」という名前のプロパティを持っていないよう

for (UIView *view in headerView.contentView.subviews) { 
     if ([view isKindOfClass:[UIButton class]]) { 
      //found button 
     } 
    } 
0

コンパイラの警告は正しいです。だから、以下のように制約コードに "goBack"ボタンを渡してみてください。

[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[goBack(40.0)]" options:0 metrics:nil views:@{@"goBack":goBack}]]; 

[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[goBack(30.0)]" options:0 metrics:nil views:@{@"goBack":goBack}]]; 

注:特定の幅と高さのためにGOBACKボタンを設定するには

[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[goBack]|" options:0 metrics:nil views:@{@"goBack":goBack}]]; 

[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[goBack]|" options:0 metrics:nil views:@{@"goBack":goBack}]]; 

:制約を追加する前headerView.translatesAutoresizingMaskIntoConstraints = false;を設定するのを忘れていない、完全にHeaderView上に占めるGOBACKボタンを設定するには

およびgoBack.translatesAutoresizingMaskIntoConstraints = false;

+0

Thx!私の最新のUpdate 1をご覧ください:比例幅を代わりに使ってみてください。 –

+0

また、headerViewにも問題があります。 "headerView.translatesAutoresizingMaskIntoConstraints = false;"を使用する場合、headerViewの位置も手動で設定する必要があります。ただし、次のコードでは、headerViewを正しく設定することはできません。私はそれを最初にtableViewに追加しようとし、UIButtonと同じ方法で設定しましたが、同じ方法では動作しませんでした。 –

+0

固定数の代わりにheaderViewまたはtableViewに比例した幅を持たせたい。私は "metrics"で "self.view.frame"(self.tableView.frameも試しました)を使用していますが、どちらも正しく動作しません。 –

関連する問題