【问题标题】:UIButton doesn't change text color when pressed按下时 UIButton 不会改变文本颜色
【发布时间】:2016-10-23 20:08:56
【问题描述】:

以编程方式创建的 UIButton 在按下时不会改变颜色,就像使用 Interface Builder 创建的所有其他按钮一样。

我的按钮创建如下:

        let cableDetailsButton = UIButton(type: UIButtonType.System)
        cableDetailsButton.frame = CGRectMake(8, 8, 42, 21)
        cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
        cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
        cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
        cableDetailsButton.addTarget(self, action: #selector(FuseDetailsViewController.cableButtonPressed), forControlEvents: .TouchUpInside)

我想补充一点,tintColor 是 iOS 的默认颜色(蓝色)

我的目标是以编程方式创建与从 Interaface Builder 列表中拖放的按钮相同的(默认!)按钮。我希望我的按钮在按下时改变颜色/alpha。

我正在使用 Swift 2.3

不幸的是,如此简单的任务让我不知所措,以至于我不得不寻求帮助。提前感谢您的帮助。

更新:按钮是 UISCrollView 的一部分

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    代替

    let cableDetailsButton = UIButton(type: UIButtonType.system)
    

    您需要将UIButtonType 更改为.custom

    let cableDetailsButton = UIButton(type: UIButtonType.custom)
    

    如果您使用的是 Swift3,那么语法也不正确。在下面找到更正的语法

    let cableDetailsButton = UIButton(type: UIButtonType.custom)
    cableDetailsButton.frame = CGRect(x: 8, y: 8, width: 42, height: 21)
    cableDetailsButton.setTitle("Dane o przewodach", for: UIControlState.normal)
    cableDetailsButton.setTitleColor(self.view.tintColor, for: .normal)
    cableDetailsButton.setTitleColor(UIColor.black, for: UIControlState.highlighted)
    cableDetailsButton.addTarget(self, action: #selector(self.cableButtonPressed), for: .touchUpInside)
    

    【讨论】:

    • 抱歉,仍然无法使用,点击后按钮不会变为黑色。我正在使用 Swift 2.3
    【解决方案2】:

    这对我有用。

        let cableDetailsButton = UIButton(type: .Custom)
        cableDetailsButton.frame = CGRect(x: 68, y: 68, width: 100, height: 70)
        cableDetailsButton.setTitle("Some button", forState: .Normal)
        cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
        cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
        view.addSubview(cableDetailsButton)
    

    或者我没有完全理解你的问题。

    【讨论】:

    • 抱歉,这对我不起作用。当我按下按钮时,我没有看到黑色。
    • 是的,它正在编译。但它不起作用。按钮永远不会改变颜色。在 iOS 10 和 iOS 9.3 上试用
    【解决方案3】:

    高亮按钮有两种方法

    方法 1

       let cableDetailsButton = UIButton(type: UIButtonType.System)
        cableDetailsButton.frame = CGRectMake(20, 50, self.view.frame.size.width - 32, 50)
        cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
        cableDetailsButton.addTarget(self, action: #selector(cableButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        cableDetailsButton.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(cableDetailsButton)
    
        func cableButtonPressed(sender: UIButton){
            dispatch_async(dispatch_get_main_queue(), {
    
                if self.isHighLighted == false{
                    sender.highlighted = true;
                    self.isHighLighted = true
                }else{
                    sender.highlighted = false;
                    self.isHighLighted = false
                }
            });
        }
    

    方法 2

        let cableDetailsButton = UIButton(type: UIButtonType.System)
        cableDetailsButton.frame = CGRectMake(20, 50, self.view.frame.size.width - 32, 50)
        cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
        cableDetailsButton.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
        cableDetailsButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        cableDetailsButton.addTarget(self, action: #selector(cableButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(cableDetailsButton)
    
    
      func cableButtonPressed(sender: UIButton){
            if sender.selected {
                // set selected
                sender.selected = true
            } else {
                // set deselected
                sender.selected = false
            }
    
        }
    

    【讨论】:

    • 按钮在点击时不会改变颜色。 iOS 9.3 iPhone 6s 模拟器。它也不能在 Swfit 2.3 中编译
    • 我更新了我的答案,请检查并告诉我它是否符合您的要求
    • 第一种方法无法编译,因为 ViewController 不包含属性 isHighLighted。第二种方法编译但不起作用。发生的唯一奇怪的事情是,在第一次单击时,按钮的 alpha 发生了变化,因此它的行为就像正常的一样。奇怪的是,它根本没有变成橙色。
    【解决方案4】:
    1. 在你的视图控制器中实现一个标志

      var buttonSelected = false
      
    2. 保留按钮的代码,但删除一行

      let cableDetailsButton = UIButton(type: UIButtonType.System)
      cableDetailsButton.frame = CGRectMake(8, 8, 42, 21)
      cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
      cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
      cableDetailsButton.addTarget(self, action: #selector(FuseDetailsViewController.cableButtonPressed), forControlEvents: .TouchUpInside)
      
    3. 在您的目标函数中添加以下内容

      func cableButtonPressed() {
          if buttonSelected {
             cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
             buttonSelected = !buttonSelected
             //do whatever else you need to do
          } else {
             cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
             buttonSelected = !buttonSelected
             //do whatever else you need to do
          }
          //do whatever else you need to do
      }
      

    【讨论】:

    • 为 UI 元素状态创建一个全局变量?没办法……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多