Re: [問題] Autolayout使用問題

看板MacDev作者 (白毛)時間11年前 (2014/02/12 09:59), 編輯推噓2(204)
留言6則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《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)

02/12 12:25, , 1F
感謝您,成功了,我之前也是這樣想...但不是用visualFormat
02/12 12:25, 1F

02/12 12:26, , 2F
我這樣寫:http://goo.gl/hN2Fa7,意思應該一樣呀?
02/12 12:26, 2F

02/12 12:26, , 3F
怎麼會這個就不成功..疑惑..
02/12 12:26, 3F
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)

02/13 22:29, , 4F
謝謝...我以為是要跟superview的bottom對齊=_=
02/13 22:29, 4F

02/13 22:30, , 5F
tableview上面跟superview的下面距離是0
02/13 22:30, 5F

02/13 22:30, , 6F
^以為是
02/13 22:30, 6F
文章代碼(AID): #1I-jMTLk (MacDev)
討論串 (同標題文章)
文章代碼(AID): #1I-jMTLk (MacDev)