【发布时间】:2017-09-08 18:51:31
【问题描述】:
我想将自定义 UIView 添加到我的应用程序的主屏幕,如果互联网连接丢失/恢复,它会显示/隐藏。我怎样才能从代码中做到这一点?
【问题讨论】:
标签: swift uiview autolayout
我想将自定义 UIView 添加到我的应用程序的主屏幕,如果互联网连接丢失/恢复,它会显示/隐藏。我怎样才能从代码中做到这一点?
【问题讨论】:
标签: swift uiview autolayout
要在 Swift 3 中隐藏视图,您可以使用:
viewVar.isHidden = true
如果您想将其与可达性检查配对,我使用找到的可达性 Swift 库 here。
我创建了一个这样的函数来检查可达性:
func CheckWiFi() -> Bool
{
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
if reachability.isReachable == true{
if reachability.isReachableViaWiFi == true{
reachability.stopNotifier()
wifi = true
return wifi
} else if reachability.isReachableViaWWAN == true{
reachability.stopNotifier()
print("Reachable via WWAN simulator")
return wifi
}else
{
reachability.stopNotifier()
print("Reachable via Cellular")
return wifi
}
}
else
{
wifi = false
reachability.stopNotifier()
print("Not reachable")
return wifi
}
}
然后为了使用它,我使用以下内容:
if CheckWiFi() == true
{
// show view here
}
else
{
// hide view here
}
【讨论】: