모든 tabBar 뷰 컨트롤러에서 progressView를 사용하여 사용자가 데이터가로드되는 양을 확인하면서 앱을 사용할 수 있도록하고 싶습니다.
내가 가진 문제는 progressView UI가 레이블 만 표시하지 않는다는 것입니다. 나는 모든 것을 올바르게 설정하고 그것이 0이 아닌지 확인한다고 생각합니다. 감사합니다.
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)
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다