从 ViewDidLoad 调用后,子视图不会删除
Subview doesn't remove after calling from ViewDidLoad
我添加了一个 UIBlurEffect
功能,可以在从服务器加载数据之前加载它。我添加了一个 boolean
参数来激活或停用它。
func addingEffectLayerWithSpinner(active: Bool){
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if active {
view.addSubview(blurEffectView)
} else {
//This one is not working
blurEffectView.removeFromSuperview()
}
}
在 viewDidLoad
中,我将 false
传递给该函数以删除 UIBlueEffect
视图,但它不起作用,你能帮我看看问题出在哪里吗.
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
addingEffectLayerWithSpinner(active: true)
profile.showingProfile { result in
switch result {
case .success(_):
// Sucesss response from server
DispatchQueue.main.async {
self.nameLabel?.text = nameOfUser!
self.avatar?.downloaded(from: avatarUrl!)
//This one is not working
self.addingEffectLayerWithSpinner(active: false)
}
// failed response from server
case .failure(let error):
print(error)
}
}
}
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
问题就在这里。由于每次调用上面的 blurEffectView 时,都会准备一个新的 UIVisualEffectView,这与现在呈现的不同。
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if active {
view.addSubview(blurEffectView)
} else {
//This one is not working
blurEffectView.removeFromSuperview()
}
所以你可以考虑将blurEffectView保存为属性然后调用移除它
var blurEffectView : UIVisualEffectView!
func addingEffectLayerWithSpinner(active: Bool){
//introduce a false condition early:
if !active { blurEffectView.removeFromSuperview()
return
}
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
blurEffectView = UIVisualEffectView(effect: blurEffect)
//remove `let` here
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
}
我添加了一个 UIBlurEffect
功能,可以在从服务器加载数据之前加载它。我添加了一个 boolean
参数来激活或停用它。
func addingEffectLayerWithSpinner(active: Bool){
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if active {
view.addSubview(blurEffectView)
} else {
//This one is not working
blurEffectView.removeFromSuperview()
}
}
在 viewDidLoad
中,我将 false
传递给该函数以删除 UIBlueEffect
视图,但它不起作用,你能帮我看看问题出在哪里吗.
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
addingEffectLayerWithSpinner(active: true)
profile.showingProfile { result in
switch result {
case .success(_):
// Sucesss response from server
DispatchQueue.main.async {
self.nameLabel?.text = nameOfUser!
self.avatar?.downloaded(from: avatarUrl!)
//This one is not working
self.addingEffectLayerWithSpinner(active: false)
}
// failed response from server
case .failure(let error):
print(error)
}
}
}
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
问题就在这里。由于每次调用上面的 blurEffectView 时,都会准备一个新的 UIVisualEffectView,这与现在呈现的不同。
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if active {
view.addSubview(blurEffectView)
} else {
//This one is not working
blurEffectView.removeFromSuperview()
}
所以你可以考虑将blurEffectView保存为属性然后调用移除它
var blurEffectView : UIVisualEffectView!
func addingEffectLayerWithSpinner(active: Bool){
//introduce a false condition early:
if !active { blurEffectView.removeFromSuperview()
return
}
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
blurEffectView = UIVisualEffectView(effect: blurEffect)
//remove `let` here
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
}