通过页面视图控制器刷新销毁布局

我有一个页面视图控制器滚动浏览embedded在导航控制器中的三个collectionViewController。 出于某种原因,当我一直向左或向右滑动并返回到第二个。 我的整个布局被破坏。

import UIKit import Firebase import FirebaseAuth class HomeViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { let dropDownLauncher = DropDownLauncher() override func viewDidLoad() { super.viewDidLoad() self.dataSource = self self.delegate = self self.automaticallyAdjustsScrollViewInsets = false if let firstViewController = viewControllerList.first{ self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil) } // setupView() } lazy var viewControllerList: [UIViewController] = { let layout = UICollectionViewFlowLayout() let homeFeedController = HomeFeedController(collectionViewLayout: layout) let navController = UINavigationController(rootViewController: homeFeedController) let profileView = ProfileeViewController(collectionViewLayout: layout) let profileViewNavController = UINavigationController(rootViewController: profileView) let searchController = EventSearchController(collectionViewLayout: layout) let searchNavController = UINavigationController(rootViewController: searchController) return [navController,profileViewNavController,searchNavController] }() override init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil) { super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: options) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { guard let vcIndex = viewControllerList.index(of: viewController) else{ return nil } let previousIndex = vcIndex - 1 guard previousIndex >= 0 else { return nil } guard viewControllerList.count > previousIndex else{ return nil } return viewControllerList[previousIndex] } func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { guard let vcIndex = viewControllerList.index(of: viewController) else{ return nil } let nextIndex = vcIndex + 1 guard viewControllerList.count != nextIndex else{ return nil } guard viewControllerList.count > nextIndex else{ return nil } return viewControllerList[nextIndex] } } 

这是通过滑动而被破坏的控制器

 import UIKit import SwiftyJSON import AlamofireImage import Alamofire import AlamofireNetworkActivityIndicator import Foundation import Firebase import FirebaseDatabase import FirebaseStorage class ProfileeViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { var profileHandle: DatabaseHandle = 0 var profileRef: DatabaseReference? let cellID = "cellID" let profileSetupTransition = AlterProfileViewController() let settingView = SettingsViewController() var userEvents = [Event]() var userId: String? var user: User? var emptyLabel: UILabel? var currentUserName: String = "" override func viewDidLoad() { super.viewDidLoad() collectionView?.backgroundColor = UIColor.white let user = self.user ?? User.current profileHandle = UserService.observeProfile(for: user) { [unowned self](ref, user, events) in self.profileRef = ref self.user = user self.userEvents = events // self.jobs = allJobs // self.reciepts = allReciepts // print(self.userEvents) // print(self.reciepts) DispatchQueue.main.async { self.collectionView?.reloadData() } } // fetchUser() self.collectionView?.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) self.navigationController?.isNavigationBarHidden = true collectionView?.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerID") collectionView?.register(EventsAttendingCell.self, forCellWithReuseIdentifier: cellID) // fetchEvents() collectionView?.alwaysBounceVertical = true } deinit { profileRef?.removeObserver(withHandle: profileHandle) } override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerID", for: indexPath) as! UserProfileHeader header.profileeSettings.addTarget(self, action: #selector(profileSettingsTapped), for: .touchUpInside) header.settings.addTarget(self, action: #selector(settingsButtonTapped), for: .touchUpInside) header.user = self.user header.backButton.addTarget(self, action: #selector(GoBack), for: .touchUpInside) return header } func GoBack(){ dismiss(animated: true, completion: nil) } func settingsButtonTapped(){ present(settingView, animated: true, completion: nil) // self.navigationController?.pushViewController(settingView, animated: true) } func profileSettingsTapped(){ present(profileSetupTransition, animated: true, completion: nil) // self.navigationController?.pushViewController(profileSetupTransition, animated: true) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 200) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.isNavigationBarHidden = true self.collectionView?.reloadData() } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if userEvents.isEmpty == false { self.collectionView?.backgroundView = nil return userEvents.count } else{ emptyLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)) let paragraph = NSMutableParagraphStyle() paragraph.lineBreakMode = .byWordWrapping paragraph.alignment = .center let attributes: [String: Any] = [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0), NSForegroundColorAttributeName: UIColor.lightGray, NSParagraphStyleAttributeName: paragraph] let myAttrString = NSAttributedString(string: "Go Attend Some Events", attributes: attributes) emptyLabel?.attributedText = myAttrString emptyLabel?.textAlignment = .center self.collectionView?.backgroundView = emptyLabel return 0 } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 1 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 1 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width = (view.frame.width - 2)/3 return CGSize(width: width, height: width) } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! EventsAttendingCell cell.layer.cornerRadius = 70/2 cell.event = userEvents[indexPath.item] return cell } } 

其余的似乎保持机智,但这只是失去了它的布局。

它不应该看起来像这样

屏幕截图1:

在这里输入图像说明 它应该看起来像这样

屏幕截图2:

在这里输入图像说明