ProgressView UI 未显示,但标签显示。 [Swift]
ProgressView UI not showing but the Label is. [Swift]
我想在所有 tabBar 视图控制器中使用我的 progressView,以便用户可以在使用该应用程序的同时查看正在加载的数据量。
我遇到的问题是 progressView UI 不只显示标签。我想我正确设置了所有内容并检查它是否为零(它不是)。请帮忙谢谢
TabBarViewController
import UIKit
let progressViewTag = 10000
let progressUpdateNotification = "progressUpdateNotification"
var progressLabel = UILabel()
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let progressView = UIProgressView(progressViewStyle: .bar)
progressView.tag = progressViewTag
progressView.frame = CGRect(x: 0, y: 93, width: self.view.frame.width, height: 5)
progressView.transform = progressView.transform.scaledBy(x: 1, y: 5)
progressView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(progressView)
progressLabel = UILabel(frame: CGRect(x: 0, y: 91, width: UIScreen.main.bounds.width, height: 8))
progressLabel.translatesAutoresizingMaskIntoConstraints = true
progressLabel.textAlignment = .center
progressLabel.font = UIFont.boldSystemFont(ofSize: 8)
progressLabel.textColor = UIColor.white
self.view.addSubview(progressLabel)
progressView.setProgress(0.0, animated: false)
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveNotification(notification:)), name: NSNotification.Name(rawValue: progressUpdateNotification), object: nil)
}
var progressView: UIProgressView? {
return self.view.viewWithTag(progressViewTag) as? UIProgressView
}
@objc func didReceiveNotification(notification: NSNotification) {
if let progress = notification.object as? ProgressNotification {
if progress.current == progress.total {
progressLabel.text = ""
self.progressView?.setProgress(0.0, animated: false)
} else {
let perc = Float(progress.current) / Float(progress.total)
progressLabel.text = "Scraping \(String(format: "%.0f", (perc * 100))) % ..."
self.progressView?.setProgress(perc, animated: true)
}
}
}
}
class ProgressNotification {
var current: Int = 0
var total: Int = 0
}
带通知的服务器调用
func apiCall(sender: UIViewController, counter: Int, completion: (()-> Void)? = nil) {
var count = counter
let total = 5
let notification = ProgressNotification()
counting(completion: { result in
count += 1
notification.current = count
notification.total = total
DispatchQueue.main.async {
NotificationCenter.default.post(name:Notification.Name(rawValue: progressUpdateNotification), object: notification)
print("notificationCenter called.")
}
.
.
.
}
}
progressView.translatesAutoresizingMaskIntoConstraints = false
改为.
progressView.translatesAutoresizingMaskIntoConstraints = true
同时更新 UIProgressView 的代码
var progressView: UIProgressView? {
return self.view.viewWithTag(progressViewTag) as? UIProgressView
}
改为.
var progressView : UIProgressView?
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.progressView = UIProgressView(progressViewStyle: .bar)
}
我想在所有 tabBar 视图控制器中使用我的 progressView,以便用户可以在使用该应用程序的同时查看正在加载的数据量。
我遇到的问题是 progressView UI 不只显示标签。我想我正确设置了所有内容并检查它是否为零(它不是)。请帮忙谢谢
TabBarViewController
import UIKit
let progressViewTag = 10000
let progressUpdateNotification = "progressUpdateNotification"
var progressLabel = UILabel()
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let progressView = UIProgressView(progressViewStyle: .bar)
progressView.tag = progressViewTag
progressView.frame = CGRect(x: 0, y: 93, width: self.view.frame.width, height: 5)
progressView.transform = progressView.transform.scaledBy(x: 1, y: 5)
progressView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(progressView)
progressLabel = UILabel(frame: CGRect(x: 0, y: 91, width: UIScreen.main.bounds.width, height: 8))
progressLabel.translatesAutoresizingMaskIntoConstraints = true
progressLabel.textAlignment = .center
progressLabel.font = UIFont.boldSystemFont(ofSize: 8)
progressLabel.textColor = UIColor.white
self.view.addSubview(progressLabel)
progressView.setProgress(0.0, animated: false)
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveNotification(notification:)), name: NSNotification.Name(rawValue: progressUpdateNotification), object: nil)
}
var progressView: UIProgressView? {
return self.view.viewWithTag(progressViewTag) as? UIProgressView
}
@objc func didReceiveNotification(notification: NSNotification) {
if let progress = notification.object as? ProgressNotification {
if progress.current == progress.total {
progressLabel.text = ""
self.progressView?.setProgress(0.0, animated: false)
} else {
let perc = Float(progress.current) / Float(progress.total)
progressLabel.text = "Scraping \(String(format: "%.0f", (perc * 100))) % ..."
self.progressView?.setProgress(perc, animated: true)
}
}
}
}
class ProgressNotification {
var current: Int = 0
var total: Int = 0
}
带通知的服务器调用
func apiCall(sender: UIViewController, counter: Int, completion: (()-> Void)? = nil) {
var count = counter
let total = 5
let notification = ProgressNotification()
counting(completion: { result in
count += 1
notification.current = count
notification.total = total
DispatchQueue.main.async {
NotificationCenter.default.post(name:Notification.Name(rawValue: progressUpdateNotification), object: notification)
print("notificationCenter called.")
}
.
.
.
}
}
progressView.translatesAutoresizingMaskIntoConstraints = false
改为.
progressView.translatesAutoresizingMaskIntoConstraints = true
同时更新 UIProgressView 的代码
var progressView: UIProgressView? {
return self.view.viewWithTag(progressViewTag) as? UIProgressView
}
改为.
var progressView : UIProgressView?
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.progressView = UIProgressView(progressViewStyle: .bar)
}