我声明了一个名为var progressBar的UIView变量:UIView?
我用它显示或隐藏视图
func showProgressBar(showBar: Bool){
if showBar{
let viewHeight = view.frame.size.height
progressBar = Bundle.main.loadNibNamed("progressBar", owner: nil, options: nil)?.first as? UIView
progressBar?.frame = CGRect(x: 0, y: viewHeight - 80 , width: self.view.frame.width, height: 80)
if let bar = progressBar{
self.view.addSubview(bar)
}
}else{
DispatchQueue.main.async() {
self.progressBar?.removeFromSuperview()
}
}
有时当我在视图之间稍微导航时,即使执行self.progressBar?.removeFromSuperview()
,我也无法隐藏进度条。在我看来,如果视图丢失了对旧进度条的引用...你对这个问题以及如何修复它有所了解吗?
答案 0 :(得分:5)
如果您两次调用showProgressBar,则不会验证是否已存在任何栏,因此您可能会添加第二个并丢失对第一个的引用。因此,当您删除它时,它只会删除第二个并且不起作用。
答案 1 :(得分:1)
如果您只想显示和隐藏 {
"name": "rats-gui",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.2.4",
"@angular/common": "^4.2.4",
"@angular/compiler": "^4.2.4",
"@angular/compiler-cli": "^4.2.4",
"@angular/core": "^4.2.4",
"@angular/forms": "^4.2.4",
"@angular/http": "^4.2.4",
"@angular/material": "^2.0.0-beta.7",
"@angular/platform-browser": "^4.2.4",
"@angular/platform-browser-dynamic": "^4.2.4",
"@angular/platform-server": "^4.2.4",
"@angular/router": "^4.2.4",
"@types/lodash": "ts2.0",
"angular2-datatable": "0.5.3",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"lodash": "^4.17.4",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"typescript": "^2.3.4",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/compiler-cli": "^4.2.4",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.28.3",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.3.0",
"typescript": "2.1.5"
}
}
,为什么不使用progressBar
?通过这样做,您可以在不再使用它时创建self.progressBar.hidden = true/false
一次和progressBar
。
removeFromSuperview