iOS View 可见性消失

标签 ios swift swift3 view swift4

我是 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 一样自动支持可见性 GONEVISIBLE

您需要 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/

相关文章:

ios - 如何使用 Google Place API 进行 mapview 搜索 Place

ios - 如何在 ScrollView 中制作表格 View ?

ios - 从 UIWebview 保存的 URL 与 True Url 不对应

swift - 如果我们在羊皮纸的 HeaderExample 中替换 UIView 而不是 UIImageView,则渐变背景不起作用

ios - SwiftyJSON解析JSON查询

ios - 这段代码使用 switch-case 和 if else if else 哪个更好?

ios - UIScrollView的 "pull up to load more"和 “pull down to go back"导致闪烁(附示例代码)

ios - Swift ios 中的自定义谷歌地图标记图标图像

ios - 缓存用户配置文件

ios - 在 iOS 中集成 FCM 以进行推送通知时,是否使用了 Device Token?