簡體   English   中英

按下時,UIButton不會更改文本顏色

[英]UIButton doesn't change text color when pressed

以編程方式創建的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

不幸的是,這個簡單的任務讓我不知所措,以至於我要求幫助。 提前感謝您的幫助。

更新:Button是UISCrollView的一部分

代替

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)

這對我有用。

    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)

或者我沒有完全理解你的問題是什么。

突出顯示按鈕有兩種方法

方法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
        }

    }
  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 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM