Xcode,由多个对象组成的设计元素

Xcode, design elements made of multiple objects

在我的应用程序中,我必须通过 Google 实现登录,为了实现这一点,我使用了 Firebase 身份验证;我设法使用带有标签的 UIView 和 UIImageView 创建了一个按钮,并向视图添加了一个 UITapGestureRecognizer。我的问题是,有没有更聪明的方法来组合元素?如果是这样,我怎样才能像按下真正的按钮一样使视图变暗?

This is the "button" I created; on the left, there's the UIImageView with Google logo and on the right, there's the label

您可以改用 UIButton。可以向按钮添加图像和标签。如果使用 Interface Builder,您只需将按钮连接到 IBAction 即可满足按下按钮的需要。 如果使用 UIButton,您也可以为不同的状态设置不同的颜色,例如突出显示(当按下按钮时)。

网上有很多教程可以实现这一点。例如这个:https://medium.com/@harmittaa/uibutton-with-label-text-and-right-aligned-image-a9d0f590bba1

你只需要在button set label中设置图片即可,语义默认设置未指定:

但是你可以改变这个按钮的语义,试试这个扩展来切换图像的语义,这个扩展功能当你试图把图像放在右边的时候,但是你可以编辑扩展到将右图与左标签分开:

extension UIButton{
func buttonSemantic(){
        titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
        imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20 / 2)
    }
}

然后你只需要设置图像和标题,并添加分隔符语义

button.setTitle("titleButton", for: UIControlState())
button.setImage(UIImage(named: "image"), for: .normal)
button.buttonSemantic()