如何在Swift中自定义Firebase身份验证UI

问题描述 投票:0回答:1

我正在使用Firebase预制UI构建身份验证系统,并且我希望自定义UI以适合程序。假设我想将背景色设置为黑色并更改按钮的角半径,有什么办法可以做到这一点?我曾尝试对authPickerViewController进行子分类,但是以某种方式无法正常工作。我进行了一些搜索,但也找不到与此相关的任何教程或近期问题。

这是我的MainViewController中的内容

class LoginViewController: UIViewController, FUIAuthDelegate{

  override func viewDidLoad() {
    super.viewDidLoad()

    let authUI = FUIAuth.defaultAuthUI()
    authUI?.delegate = self
    let providers: [FUIAuthProvider] = [
      FUIEmailAuth(),
      FUIGoogleAuth(),
      FUIPhoneAuth(authUI:FUIAuth.defaultAuthUI()!)]
    authUI?.providers = providers

    let authViewController = authUI?.authViewController()
    authViewController!.modalPresentationStyle = .fullScreen
    authViewController!.setNavigationBarHidden(true, animated: false)

    self.present(authViewController!, animated: false, completion: nil)
  }

  func application(_ app: UIApplication, open url: URL,
    options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication]
    if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication as? String) ?? false {
      return true
    }
    return false
  }
}

这是我创建的子类:

class FUICustomAuthPickerViewController: FUIAuthPickerViewController,FUIAuthDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = .black
  }

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

Firebase documentation for customization上,他们说:

您可以通过子类化FirebaseUI的视图控制器并在FUIAuth的委托方法中指定它们来自定义登录屏幕。

我是初学者,我该怎么做?

编辑:

因此,按照this link上的说明,我设法通过创建FUIAuthDelegate的扩展名将内容添加到预构建的UI。>

extension LoginViewController:FUIAuthDelegate {
  func application(_ app: UIApplication, open url: URL,
    options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication]
    if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication as? String) ?? false {
      return true
    }
    return false
  }

  func authPickerViewController(forAuthUI authUI: FUIAuth) -> FUIAuthPickerViewController {
    let vc = FUIAuthPickerViewController(authUI: authUI)

    let view = UIView(frame: .zero)
    view.backgroundColor = .black
    view.translatesAutoresizingMaskIntoConstraints = false
    vc.view.addSubview(view)

    NSLayoutConstraint.activate([
      view.heightAnchor.constraint(equalTo: vc.view.heightAnchor, multiplier: 1),
      view.widthAnchor.constraint(equalTo: vc.view.widthAnchor, multiplier: 1)])

    return vc
  }
}

不一定需要子类。但是,我似乎无法使我创建的该视图成为背景,它要么覆盖所有内容,要么根本不覆盖任何内容。我尝试直接更改视图的背景颜色,但没有用。有人知道该怎么做吗?

我正在使用Firebase预制UI构建身份验证系统,并且我希望自定义UI以适合程序。假设我想将背景色设置为黑色并更改拐角半径...

swift firebase-authentication firebaseui
1个回答
0
投票

使用此方法和此link中提供的注释之一解决了问题。事实证明,除了子类化以外,您还必须在子类中添加以下两个方法才能使其起作用。

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, authUI: FUIAuth?) {
        super.init(nibName: nil, bundle: Bundle.main, authUI: authUI!)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
© www.soinside.com 2019 - 2024. All rights reserved.