作者whitefur (白毛)
看板MacDev
标题Re: [问题] Autolayout使用问题
时间Wed Feb 12 09:59:54 2014
※ 引述《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)
1F:推 kiii210:感谢您,成功了,我之前也是这样想...但不是用visualFormat 02/12 12:25
3F:→ 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)
4F:推 kiii210:谢谢...我以为是要跟superview的bottom对齐=_= 02/13 22:29
5F:→ kiii210:tableview上面跟superview的下面距离是0 02/13 22:30
6F:→ kiii210:^以为是 02/13 22:30