【问题标题】:The effect of pressing the button [closed]按下按钮的效果[关闭]
【发布时间】:2023-03-31 00:07:02
【问题描述】:

我该怎么做,当你点击它时它会变暗并返回,但文本总是可见的。

【问题讨论】:

  • 请详细说明一下。
  • 使用文本的颜色代码作为按钮的背景颜色和屏幕的背景颜色作为按钮的背景颜色。
  • @AdityaSrivastava 点击时按钮变暗是必须的
  • 在 uibutton 操作中将按钮的背景颜色更改为 superview 的颜色

标签: ios swift button xcode8


【解决方案1】:

将按钮设置为自定义类型,并自定义高亮和选中状态:

【讨论】:

    【解决方案2】:

    用户点击时更改背景和文本颜色。

    【讨论】:

      【解决方案3】:

      您可以创建一个UIButton 子类并为不同的控件状态设置背景图像:

      class CustomButton: UIButton {
      
          override init(frame: CGRect) {
              super.init(frame: frame)
              sharedInit()
          }
      
          required init?(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
              sharedInit()
          }
      
          private func sharedInit() {
              setBackgroundImage(UIColor.darkGrayColor().image, forState: .Normal)
              setBackgroundImage(UIColor.blackColor().image, forState: .Highlighted)
              setTitleColor(UIColor.whiteColor(), forState: .Normal)
          }
      
      }
      
      extension UIColor {
      
          var image: UIImage {
              return imageWithSize(CGSize(width: 1, height: 1))
          }
      
          func imageWithSize(size: CGSize) -> UIImage {
              UIGraphicsBeginImageContextWithOptions(size, true, 0)
      
              self.setFill()
              UIRectFill(CGRect(origin: .zero, size: size))
      
              let image = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
      
              return image
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多