我有UIPageViewController
来处理viewControllers
数组
当我在它们之间滑动时,一切都很好,但我有一些方法(成功登录后和菜单按钮点击后)触发“跳跃”。到viewController
数组中的UIPageViewControllers
。
每当我跳转到一个页面时,过渡都没有完成,而且我的屏幕被卡住了。在两个viewControllers
之间
为简单起见,我只会添加成功的登录代码,因为我为按钮点击设置了相同的内容,同样的事情发生了。
这是我的UIPageViewController设置:
import UIKit
class PageViewController: UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate , ClubViewControllerDelegate , CustomSvDelegate{
//clubvcdelegate protocol method
func fbLoggedInWithSuccess(){
self.displayPageForIndex(index: 4, animated: false)
}
//menubuttondelegate protocol method
func menuButtonPressed0() {
self.displayPageForIndex(index: 0, animated: false)
self.currentPageIndex = 0
self.closeMenu()
}
//page swipe control
lazy var vcArray : [UIViewController] = {
return [self.vcInstance (name : "clubVC"),
self.vcInstance (name : "menuVC"),
self.vcInstance (name : "contactVC"),
self.vcInstance (name : "aboutVC"),
self.vcInstance (name : "membersVC")]
}()
private func vcInstance(name : String) -> UIViewController{
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
return storyBoard.instantiateViewController(withIdentifier: name)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let clubVC = vcArray.first {
let club = clubVC as! ClubViewController
club.delegate = self
setViewControllers([clubVC], direction: .forward, animated: true, completion: nil)
}
self.generateStackView()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for view in self.view.subviews {
if view is UIScrollView {view.frame = UIScreen.main.bounds} else if view is UIPageControl {view.backgroundColor = UIColor.clear}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {return vcArray.last}
guard vcArray.count > previousIndex else {return nil}
return vcArray[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = vcArray.index(of: viewController) else {return nil}
let nextIndex = viewControllerIndex + 1
guard nextIndex < vcArray.count else {return vcArray.first}
guard vcArray.count > nextIndex else {return nil}
return vcArray[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return vcArray.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let firstViewController = viewControllers?.first ,
let firstViewControllerIndex = vcArray.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
这就是我设置fblogin成功的方法:
protocol ClubViewControllerDelegate:class {
func fbLoggedInWithSuccess()
}
class ClubViewController: UIViewController , FBSDKLoginButtonDelegate {
var delegate:ClubViewControllerDelegate?
func showFbDetails(){
let accesToken = FBSDKAccessToken.current()
guard let accesTokenString = accesToken?.tokenString else {return}
let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accesTokenString)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if error != nil {
print ("something went wrong" , error!)
return
}
print ("succes")
self.delegate?.fbLoggedInWithSuccess()
})
}
EDIT2 这只有在动画为true时才会发生,如果false则正确完成
EDIT3 这是我现在用来设置视图控制器的方法:
func displayPageForIndex(index : Int , animated : Bool = true){
assert(index >= 0 && index < self.vcArray.count, "Error: Attempting to display a page for an out of bounds index")
if self.currentPageIndex == index {return}
if index < self.currentPageIndex {
self.setViewControllers([self.vcArray[index]], direction: .reverse, animated: false, completion: nil)
} else if index > self.currentPageIndex {
self.setViewControllers([self.vcArray[index]], direction: .forward, animated: false, completion: nil)
}
}
我在fbLoggenInWithSuccess
和menuButtonPressed
内召唤它。
此外,还有一个包含页面当前索引的变量CurrentIndex
答案 0 :(得分:0)
如亚特建议删除viewDidLayoutSubviews
解决了问题