我是 iOS 开发的新手。我想从父 View 切换(隐藏/可见) subview 。在 android 中,有一种方法可以隐藏可见性。
在安卓中
subView.setVisibility(View.GONE);
在 iOS 中
subView.removeFromSuperview()
当我使用上面的函数时,它删除了 subViewConstraints 并弄乱了我的 ScrollView 约束。
topsubView.bottomAnchor.constraint(equalTo: bottomSubView.topAnchor, constant: 8).isActive = true
当我使用上面的代码时,它工作正常并隐藏了 subView。但是当我想让 subView 可见时,它没有显示 subView。
topsubView.bottomAnchor.constraint(equalTo: bottomSubView.topAnchor, constant: 8).isActive = false
self.view.layoutIfNeeded()
希望您理解我的问题。提前致谢。
最佳答案
因为我在 iOS 和 Android 上都工作过,所以您需要在 iOS 中使用约束 socket 来实现 Android 功能。 iOS 不支持像 Android native 一样自动支持可见性 GONE
和 VISIBLE
您需要 Hook 特定约束
的outlet
(可能是垂直/水平/高度)您需要将其设置为0
&需要管理您的用户界面。
隐藏:
self.viewYourConstraint.constant = 0
self.yourView.hidden = true
self.view.layoutIfNeeded()
显示:
self.viewYourConstraint.constant = 100//your constant value
self.yourView.hidden = false
self.view.layoutIfNeeded()
Note: If other constraints will be affected because of the update to the constraint above, the following must also need to be called:
self.yourView.setNeedsUpdateConstraints()
关于iOS View 可见性消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48622926/