在視圖控制器中,最接近self.navigationController.navigationBar
的祖先和self.view
的子視圖是什麼,所以我可以將約束添加到該祖先?用於設置自動佈局約束的iOS視圖層次
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?
謝謝!
有沒有你不能讓你的視圖的頂部和搜索欄的頂部之間約束的原因是什麼?您的視圖的頂部應在導航欄的底部,所以,應該給你想要的東西。 – rdelmar