0

ビューコントローラで最も近い祖先self.navigationController.navigationBarとのサブビューはself.view何ですか?その祖先に制約を追加できますか?

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[self.searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.searchBar];

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:self.searchBar  
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom 
multiplier:1.0 constant:0];

やってみた

[self.view addConstraint:cn];
[self.navigationController.view addConstraint:cn];

しかし、どちらの場合も、「制約はビューのサブツリーの外側から何かを参照していますか?それは違法です」というエラーが表示されます。

誰かがnavigationControllerに関連してViewControllerのビュー階層を説明できますか?

ありがとう!

4

1 に答える 1

1

ビューツリーは次のようになります。

ここに画像の説明を入力してください

したがって、navigationController.viewは、ナビゲーションバーとビューの最も近い祖先です。なぜそれがうまくいかなかったのかわかりません。そのコードをどこに置いたのですか?

于 2013-01-19T17:00:18.827 に答える