segue 后屏幕变黑
Screen goes black after segue
我试过调试这个但没有成功。
基本上,当我从第一个视图控制器转到第二个视图控制器时,屏幕会暂时变黑。代码按我想要的方式执行,但黑屏对我来说有点痛苦。
代码如下:
从第一页开始的转折:
func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) {
self.performSegue(withIdentifier: "goToSecond", sender: self)
}
第二个视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
self.loadDataFromFirebase()
}
第一个函数:
func loadDataFromFirebase() {
let db = Firestore.firestore()
db.collection(restaurauntName).getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
return
} else {
for document in snapshot!.documents {
let name = document.get("Name") as! String
let description = document.get("Description") as! String
self.names.append(name)
self.descriptions.append(description)
}
self.setupImages() //safe to do this here as the firebase data is valid
self.collectionView?.reloadData()
}
}
}
此函数设置页面布局
func setupImages(){
self.pages = [
Page(imageName: self.imagesOne, headerText: names[0], bodyText: descriptions[0]),
Page(imageName: self.imagesTwo, headerText: names[1], bodyText: descriptions[1]),
Page(imageName: self.imagesThree, headerText: names[2], bodyText: descriptions[2]),
Page(imageName: self.imagesFour, headerText: names[3], bodyText: descriptions[3]),
Page(imageName: self.imagesFive, headerText: names[4], bodyText: descriptions[4]),
]
self.collectionView?.backgroundColor = .white
self.collectionView?.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
self.collectionView?.isPagingEnabled = true
}
这将设置页面控件
lazy var pageControl: UIPageControl = {
let pc = UIPageControl()
pc.currentPage = 0
pc.numberOfPages = 5
pc.currentPageIndicatorTintColor = .red
pc.pageIndicatorTintColor = .gray
return pc
}()
滑动控制器扩展:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell
let page = pages[indexPath.item]
cell.page = page
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
如果我需要添加任何其他内容,请告诉我!欢迎任何帮助
这是问题的 YouTube 剪辑:
https://www.youtube.com/watch?v=vQGiw3Jd9pM
后来我发现,当我注释掉 firebase 方法时,问题就消失了。
您的 loadDataFromFirebase() 方法似乎需要很长时间才能在主线程上执行,导致它挂起。将您的提取移动到后台线程并在数据被提取后更新您的 UI 同时显示加载指示器或类似的东西。尝试:
func loadDataFromFirebase() {
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
// Fetch and convert data
let db = Firestore.firestore()
...
DispatchQueue.main.async {
// Update your UI components with the data here
self.setupImages()
self.collectionView?.reloadData()
}
}
}
问题我解决了!问题是调用函数的顺序。
我没有意识到页面的设置对于 firebase 来说太快了,所以我现在更早地调用了该函数!在视图中会出现
override func viewWillAppear(_ animated: Bool) {
print("Done")
super.viewWillAppear(animated)
self.loadDataFromFirebase()
}
感谢所有帮助过的人!
我试过调试这个但没有成功。
基本上,当我从第一个视图控制器转到第二个视图控制器时,屏幕会暂时变黑。代码按我想要的方式执行,但黑屏对我来说有点痛苦。
代码如下:
从第一页开始的转折:
func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) {
self.performSegue(withIdentifier: "goToSecond", sender: self)
}
第二个视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
self.loadDataFromFirebase()
}
第一个函数:
func loadDataFromFirebase() {
let db = Firestore.firestore()
db.collection(restaurauntName).getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
return
} else {
for document in snapshot!.documents {
let name = document.get("Name") as! String
let description = document.get("Description") as! String
self.names.append(name)
self.descriptions.append(description)
}
self.setupImages() //safe to do this here as the firebase data is valid
self.collectionView?.reloadData()
}
}
}
此函数设置页面布局
func setupImages(){
self.pages = [
Page(imageName: self.imagesOne, headerText: names[0], bodyText: descriptions[0]),
Page(imageName: self.imagesTwo, headerText: names[1], bodyText: descriptions[1]),
Page(imageName: self.imagesThree, headerText: names[2], bodyText: descriptions[2]),
Page(imageName: self.imagesFour, headerText: names[3], bodyText: descriptions[3]),
Page(imageName: self.imagesFive, headerText: names[4], bodyText: descriptions[4]),
]
self.collectionView?.backgroundColor = .white
self.collectionView?.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
self.collectionView?.isPagingEnabled = true
}
这将设置页面控件
lazy var pageControl: UIPageControl = {
let pc = UIPageControl()
pc.currentPage = 0
pc.numberOfPages = 5
pc.currentPageIndicatorTintColor = .red
pc.pageIndicatorTintColor = .gray
return pc
}()
滑动控制器扩展:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell
let page = pages[indexPath.item]
cell.page = page
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
如果我需要添加任何其他内容,请告诉我!欢迎任何帮助
这是问题的 YouTube 剪辑:
https://www.youtube.com/watch?v=vQGiw3Jd9pM
后来我发现,当我注释掉 firebase 方法时,问题就消失了。
您的 loadDataFromFirebase() 方法似乎需要很长时间才能在主线程上执行,导致它挂起。将您的提取移动到后台线程并在数据被提取后更新您的 UI 同时显示加载指示器或类似的东西。尝试:
func loadDataFromFirebase() {
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
// Fetch and convert data
let db = Firestore.firestore()
...
DispatchQueue.main.async {
// Update your UI components with the data here
self.setupImages()
self.collectionView?.reloadData()
}
}
}
问题我解决了!问题是调用函数的顺序。
我没有意识到页面的设置对于 firebase 来说太快了,所以我现在更早地调用了该函数!在视图中会出现
override func viewWillAppear(_ animated: Bool) {
print("Done")
super.viewWillAppear(animated)
self.loadDataFromFirebase()
}
感谢所有帮助过的人!