2017-09-06 78 views
1

我想在登录屏幕的登录按钮后添加一个图像。目前,我得到了以下错误消息:FirebaseAuthUI IOS - 如何添加登录屏幕背景图片?

终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“在包无法加载NIB:”一个NSBundle < ...>与名称...

这里我的代码:

在AppDelegate中:

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    var authHandle: AuthStateDidChangeListenerHandle? 
    var userInfo:User! 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     FirebaseApp.configure() 

     //checkLoggedIn 
     authHandle = Auth.auth().addStateDidChangeListener { auth, user in 
      if let user = user { 
       self.userInfo = user 
      } else { 
       // No user is signed in. 
       self.login() 
      } 
     } 

     return true 
    } 

func login() { 
     let authUI = FUIAuth.defaultAuthUI() 
     authUI?.delegate = self as? FUIAuthDelegate 

     let providers: [FUIAuthProvider] = [ 
      FUIFacebookAuth() 
     ] 
     authUI?.providers = providers 

     // Present the auth view controller and then implement the sign in callback. 
     let authViewController = CustomAuthViewController(authUI: authUI!) 
     self.window?.rootViewController?.present(authViewController, animated: false, completion: nil) 
    } 

    func authPickerViewController(for authUI: FUIAuth) -> FUIAuthPickerViewController { 
     return CustomAuthViewController(nibName: "CustomAuthViewController", 
             bundle: Bundle.main, 
             authUI: authUI) 
    } 
} 

在CustomAuthViewController:

class CustomAuthViewController: FUIAuthPickerViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     let width = UIScreen.main.bounds.size.width 
     let height = UIScreen.main.bounds.size.height 

     let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height)) 
     imageViewBackground.image = UIImage(named: "myimage") 

     // you can change the content mode: 
     imageViewBackground.contentMode = UIViewContentMode.scaleToFill 

     view.insertSubview(imageViewBackground, at: 0) 
    } 

你看到有什么问题吗?我不确定authPickerViewController函数应该在我的AppDelegate中,你能确认它没问题吗?我发现this后和this one但我不能让它工作。 我使用Firebase-UI 4.1.1,我的登录流程没有背景图片(所以没有CustomPickerViewController)。

谢谢

亚历

+0

看看这个[answer](https://stackoverflow.com/a/13931118/4056108) – chirag90

+0

或者即使这[回答](https://stackoverflow.com/a/28037785/4056108) – chirag90

+0

谢谢你的这些链接!但我不知道如何解决我的问题,因为它与FirebaseAuthUi有关。所以我的故事板中没有任何控制器用于登录,它由Firebase加载并处理。如果我用AppDelegate中的authViewController = authUI!.authViewController()替换let authViewController = CustomAuthViewController(authUI:authUI!),登录流程就可以工作,但我没有任何背景图像。清理项目不起作用。 – Alex9494

回答

0

这就是我的个性覆盖的样子:

import UIKit 
import FirebaseAuthUI 

class BizzyAuthViewController: FUIAuthPickerViewController { 


    override init(nibName: String?, bundle: Bundle?, authUI: FUIAuth) { 
     super.init(nibName: "FUIAuthPickerViewController", bundle: bundle, authUI: authUI) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     let width = UIScreen.main.bounds.size.width 
     let height = UIScreen.main.bounds.size.height 

     let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height)) 
     imageViewBackground.image = UIImage(named: "lavenderWithBees") 

     // you can change the content mode: 
     imageViewBackground.contentMode = UIViewContentMode.scaleAspectFill 

     view.insertSubview(imageViewBackground, at: 0) 

    } 

} 

而这正是我的登录功能( “ViewController.swift” 内)是这样的:

func login() { 

    //FirebaseApp.configure() 
    let authUI = FUIAuth.defaultAuthUI() 
    let providers: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFacebookAuth()] 
    authUI?.providers = providers 
    authUI?.delegate = self as FUIAuthDelegate 

    let authViewController = BizzyAuthViewController(authUI: authUI!) 
    let navc = UINavigationController(rootViewController: authViewController) 
    self.present(navc, animated: true, completion: nil) 

}