无法将UIButton的自定义类更改为GIDSignInButton

时间:2017-12-24 11:22:48

标签: ios swift uibutton interface-builder google-signin

我尝试将Google登录功能添加到自定义按钮,并且我正在关注this教程。它告诉我选择按钮,并将其类设置为GIDSignInButton,然后添加以下代码:@IBOutlet weak var signInButton: GIDSignInButton!

但是,它不允许我将课程设置为GIDSignInButton。当我输入并按回车键时,该字段就会清除。

enter image description here

11 个答案:

答案 0 :(得分:4)

你应该尝试不将GIDSignInButton从对象库分配给Button对象,而是分配给View对象

这对我有用。

答案 1 :(得分:4)

您可以创建UIButton,然后在其操作方法上编写以下代码以通过Google进行签名:

    GIDSignIn.sharedInstance().signIn()

它对我有用,通过这种方式,您可以根据需要自定义UIButton,还可以使用Google进行登录

答案 2 :(得分:4)

这是因为GIDSignInButton是UIView的子类,而不是UIButton。 在情节提要/笔尖中添加常规UIView,然后将其类更改为GIDSignInButton。

来自google doc

  

将GIDSignInButton添加到情节提要,XIB文件或实例化它   以编程方式要将按钮添加到情节提要或XIB文件中,   添加视图并将其自定义类设置为GIDSignInButton。

答案 3 :(得分:3)

解决方法文字模式中打开storyboard并直接放置。当您返回interface builder时,它将正常显示。

答案 4 :(得分:1)

几个月前我遇到了同样的问题,

您的代码似乎是正确的

@IBOutlet weak var signInButton: GIDSignInButton!

但问题可能是

•您尚未正确添加框架 (转到左侧导航器中的项目设置,然后单击构建阶段,添加框架和SHIFT + CMD + K)

•或者,继续在swift文件中写下@IBOutlet,然后拖动按钮进行分配

•您的最后一个选项是关闭xcode,或者删除派生数据

Xcode本身有很多错误,我不确定这是不是你的问题,它是xcode的

希望这有帮助!

答案 5 :(得分:1)

  1. 打开情节提要作为源代码。
  2. 在xml中找到按钮。
  3. 将customClass =“ GIDSignInButton”设置为按钮标签的属性。
  4. 打开情节提要作为界面生成器。

您现在可以将按钮链接到IBOutlet

答案 6 :(得分:1)

使用 UIView 代替 UIButton ,并将自定义类分配为 GIDSignInButton

答案 7 :(得分:0)

如果使用UIView而不是UIButton,则可以为视图分配GIDSignInButton的自定义类。从那里可以将视图连接到按钮插座和操作,如下所示。

@IBOutlet weak var googleLoginButton: GIDSignInButton!

  @IBAction func googleLoginButtonPressed(_ sender: Any) {
    GIDSignIn.sharedInstance()?.signIn()
}

答案 8 :(得分:0)

可以使用UIView或UIButton设置GIDSignInButton。

如果您将GIDSignInButton用作UIButton

  1. 打开情节提要作为源代码
  2. 在结果XML中找到按钮
  3. 将以下代码添加为按钮标签的属性

    customClass =“ GIDSignInButton”

4。再次打开Storyboard作为Interface Builder,按钮类将更改

如果您将GIDSignInButton用作UIView

1.copy将自定义类粘贴为 GIDSignInButton 身份检查器

第二个是我认为正确的方法

答案 9 :(得分:0)

Swift- 5

Open storyboard as source code.
Locate the button in the xml.
Set customClass="GIDSignInButton" as an attribute for the button tag.
Open storyboard as interface builder.

//MARK:- You will not find this with button XML Forcly Add this (customClass="GIDSignInButton")[![enter image description here][1]][1]

答案 10 :(得分:0)

简单的方法是执行一个按钮动作,然后在其中粘贴以下几行。

   @IBAction func gSignInAction(_ sender: Any) {
         GIDSignIn.sharedInstance()?.signIn()
    }