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

标签 ios swift firebase-authentication firebaseui

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

由于未捕获的异常“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)。

谢谢,

亚历克斯

最佳答案

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

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)

}

关于ios - FirebaseAuthUI IOS - 如何添加登录屏幕背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070531/

相关文章:

ios - CloudKit - 如果记录不存在如何保存

带有Xcode 11.2.1的swift_initClassMetadataImpl EXC_BAD_ACCESS

ios - 如何在 UICollectionView、Swift 5、Xcode 的行下添加行

node.js - 调用 `getUser` 时发生 firebase-admin auth 错误,但调用 verifyToken 时工作正常

Swift & Firebase 无法将类型 'AuthDataResult?' 的值分配给类型“用户?

ios - 如何获取主机名/IP 地址的 CNAME

ios - 使用 Sinch API 和 iOS SDK 获取对话列表

ios - Parse.com 查询和应用程序设计问题 : A long-running operation is being executed on the main thread

Swift 3 GCD 和解析

javascript - VueJS - 变量已定义但从未使用过(将数据发送到 Firebase)