UIbutton按下时未突出显示 [英] UIbutton is not highlighting when pressed

查看:139
本文介绍了UIbutton按下时未突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UIButton实例在按下时未突出显示.我很确定我的代码是正确的.这仅在我正在使用的此特定类中发生.我猜测是因为该功能已被我所遵循的协议所覆盖.

My UIButton instance is not highlighting when pressed. I'm pretty sure my code is correct. This only happens in this specific class that I am using. I'm guessing because the function has been overridden by a protocol that I am conforming to.

我想使UIButton突出显示默认方式,但是我必须手动进行.如何在按下按钮时强制使其高亮显示,以及按钮的默认设置(就颜色而言)是什么,以使其与所有其他按钮保持一致?

I want to make the UIButton highlight the default way, but I have to do it manually. How can I force highlight the button when pressed, and what are the default settings for it (in terms of colour), so that it is consistent with all the other buttons?

这是我当前的代码:

    let backButton = UIButton()
    backButton.setTitle("back", for: .normal)
    backButton.setTitleColor(UIColor.black, for: .normal)
    backButton.adjustsImageWhenHighlighted = true
    RevealBar.addSubview(backButton)
    backButton.snp.makeConstraints { (make) -> Void in
        make.center.equalTo(RevealBar)
        make.width.equalTo(RevealBar)
        make.height.equalTo(RevealBar)
    }

    backButton.addTarget(self, action: #selector(self.goBack), for: .touchUpInside)

推荐答案

使用UIButton(type buttonType: .system)而不是UIButton()初始化按钮.

Initialize the button using UIButton(type buttonType: .system) instead of UIButton().

使用后者时,由于默认按钮类型为UIButtonType.custom,因此无法正确设置按钮高亮显示,该样式将用作空白样式,而没有任何默认样式或突出显示行为.

When you use the latter it won't properly set up button highlighting because the default button type is UIButtonType.custom, which is intended as a blank slate without any of the default styling or highlighting behaviour.

注意,摘自 Apple的UIButton文档:

Note, from Apple's UIButton documentation:

按钮的类型定义其基本外观和行为.您指定 使用 init(type :)方法创建时按钮的类型,或者 在情节提要文件中. 创建按钮后,您将无法更改 .最常用的按钮类型是自定义"和 系统类型,但在适当时使用其他类型.

A button’s type defines its basic appearance and behavior. You specify the type of a button at creation time using the init(type:) method or in your storyboard file. After creating a button, you cannot change its type. The most commonly used button types are the Custom and System types, but use the other types when appropriate.

这篇关于UIbutton按下时未突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆