IOS如何完全删除视图

时间:2018-05-14 07:32:32

标签: ios iphone

我想完全隐藏视图。我是ios的新手。但我在android方面有很棒的工作经验。

所以在android中我们可以设置可见性消失。它完全从布局中删除了视图。我想在IOS中做同样的事情。这是布局/设计实例

查看1 查看2(这是我要隐藏和显示的那个) 查看3

现在,当我想要隐藏视图2时,我希望视图2的空间也从屏幕消失,视图1和视图3必须粘在一起。当视图2设置为可见时,它必须按顺序显示。即查看1,2,3

现在我正在做的是设置 view2.ishidden = true ,但它不按我想要的方式工作。

  

请告诉我什么相当于IOS中android的view.gone。 ???

1 个答案:

答案 0 :(得分:2)

有几种方法可以实现这一目标,但您在这里缺少的是关于如何设置布局的一些关键信息。

我将假设你所拥有的3个视图是垂直对齐的,彼此相邻并具有相等的宽度。

我们从横向角度以编程方式看待这一点:

use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'https://www.fairprice.com.sg/searchterm/apple');
foreach ($crawler->filter('a.pdt_title') as $node) {
    print $node->nodeValue."\n";
}

当第二个被跳过时,我们可能只需停用let firstLeading = NSLayoutConstraint(item: view1, attribute: .leading, relatedBy: .equal, toItem: parent, attribute: .leading, multiplier: 1.0, constant: 0.0) let betweenSecondAndFirst = NSLayoutConstraint(item: view2, attribute: .leading, relatedBy: .equal, toItem: view1, attribute: .trailing, multiplier: 1.0, constant: 0.0) let secondEqualWidth = NSLayoutConstraint(item: view2, attribute: .width, relatedBy: .equal, toItem: view1, attribute: .width, multiplier: 1.0, constant: 0.0) let betweenThirdAndSecond = NSLayoutConstraint(item: view3, attribute: .leading, relatedBy: .equal, toItem: view2, attribute: .trailing, multiplier: 1.0, constant: 0.0) let lastEqualWidth = NSLayoutConstraint(item: view3, attribute: .width, relatedBy: .equal, toItem: view1, attribute: .width, multiplier: 1.0, constant: 0.0) // Note to view1 to make things easier let lastTrailing = NSLayoutConstraint(item: view3, attribute: .trailing, relatedBy: .equal, toItem: parent, attribute: .trailing, multiplier: 1.0, constant: 0.0) 并将betweenThirdAndSecond添加为:

betweenThirdAndFirst

您可以使用约束属性来启用或禁用它们。或者您可以简单地使用优先级并将其切换为例如900到100.您可以在故事板中实际设置所有这些约束,然后将2作为插件拖到代码中。然后只需:

let betweenThirdAndFirst = NSLayoutConstraint(item: view3, attribute: .leading, relatedBy: .equal, toItem: view1, attribute: .trailing, multiplier: 1.0, constant: 0.0)

这种方式可能是您可以控制的最佳方式(主要是动画)。但是你也可以使用func setMiddleViewShown(_ shown: Bool) { UIView.animate(withDuration: 0.3) { self.betweenThirdAndSecond.priority = UILayoutPriority(rawValue: shown ? 900.0 : 100.0) self.betweenThirdAndFirst.priority = UILayoutPriority(rawValue: shown ? 100.0 : 900.0) self.view2.alpha = shown ? 1.0 : 0.0 parent.layoutIfNeeded() // parent is most likely "self.view" } } 方法来插入或删除视图。 UIStackView应该也可以。或者您知道..只需以编程方式完成所有操作,忽略约束并为每个视图设置UICollectionView

我在使用frame时可以考虑的最小值如下,并且有效:

UIStackView
相关问题