看板 MacDev 關於我們 聯絡資訊
※ 引述《kiii210 (JohnnyChian)》之銘言: : 大家好,上來請教一下各位高手...小弟在Autolayout使用上一直都很有困難 : 現在是用StoryBoard拉Autolayout : 畫面上有一個 "searchBar" 跟 "tableView" 排列方式如圖 : http://i.imgur.com/RrlzpSr.png : searchBar的constraint是Bottom Space to tableView, 距離0 : tableView的constraint是Top Space to tableView, 距離0 : 另外也有Leading space 跟 Trailing Space to Superview, 距離也都是0 : 現在有個需求, 有某個狀況會要把searchBar從畫面中移除(removeFromSuperView) : 而我想要在searchBar移除之後讓tableView的Top Space to Superview 是 0 : 簡單的說就是searchBar移除之後, tableView要往上遞補.. : 不過一直試不出來, 也不知道constraint怎麼設比較好 : 不知道searchBar移除之後, 該怎麼讓autolayout知道我想重新layout @@ : 麻煩一下各位高手了 謝謝 設一個讓tableView的top跟superview的bottuom距離為0的constraint即可 ^^^^我寫錯了,是top [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableView]" options:0 metrics:nil views:@{@"tableView": _tableView}]]; 我寫了一個簡單的Demo https://github.com/WhiteFur/AutolayoutPractice -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.116.75 ※ 編輯: whitefur 來自: 118.160.116.75 (02/12 10:14)
kiii210:感謝您,成功了,我之前也是這樣想...但不是用visualFormat 02/12 12:25
kiii210:我這樣寫:http://goo.gl/hN2Fa7,意思應該一樣呀? 02/12 12:26
kiii210:怎麼會這個就不成功..疑惑.. 02/12 12:26
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom ^^^^^^ Top才對 因為是superview的top跟tableView對齊 multiplier:1.0 constant:0.0f]; ※ 編輯: whitefur 來自: 61.230.125.241 (02/12 16:18) ※ 編輯: whitefur 來自: 61.230.125.241 (02/12 16:18) ※ 編輯: whitefur 來自: 61.230.125.241 (02/12 16:19)
kiii210:謝謝...我以為是要跟superview的bottom對齊=_= 02/13 22:29
kiii210:tableview上面跟superview的下面距離是0 02/13 22:30
kiii210:^以為是 02/13 22:30