FirebaseAuthUI IOS - 如何添加登录屏幕背景图片?

时间:2017-09-06 08:40:11

标签: ios swift firebase-authentication firebaseui

我想在登录屏幕的登录按钮后面添加一个图像。我目前收到以下错误消息:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在bundle中加载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)。

谢谢,

亚历

1 个答案:

答案 0 :(得分:1)

这是我的自定义覆盖的样子:

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)

}