ios - 将按钮的文本颜色从黑色更改为白色,然后恢复原状

标签 ios swift uibutton uicontrolstate

我在滑出式菜单中有十个按钮。如何通过选择将十个按钮的文本颜色从黑色更改为白色,然后在用户单击另一个按钮时恢复到原始状态。

我一个一个地循环按钮,我使用了代码

@IBAction func onBtnClick(sender: UIButton) {

        hmImg.hidden = true
    editprofileImg.hidden = true
    cntctsReqImg.hidden = true
    cntctsManImg.hidden = true
    preferencesImg.hidden = true
    timeRuleImg.hidden = true
    helpImg.hidden = true
    logoutImg.hidden = true


    var a: NSInteger = sender.tag

    if a == 1
    {


        homeBtn.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
        hmImg.hidden = false


        }

    else if a == 2
    {
        editProfileBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        editprofileImg.hidden = false

    }
   else if a == 3
    {
        cntctsRequetsBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        cntctsReqImg.hidden = false

    }

   else if a == 4


    {
        cntctMangBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
         cntctsManImg.hidden = false

    }
 else   if a == 5
    {
        preferenceBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        preferencesImg.hidden = false
               }

  else  if a == 6
    {
        timeruleBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        timeRuleImg.hidden = false

    }

  else  if a == 7
    {
        helpBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        helpImg.hidden = false

    }


  else  if a == 8
    {
        logoutBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

        logoutImg.hidden = false

    }

此状态的问题是当我选择其他按钮时按钮颜色保持白色。当用户选择另一个按钮时,我想将文本颜色更改为黑色

当我点击按钮时,选择的控件状态几乎不会出现几秒钟

最佳答案

试试这个:

let buttons = [homeBtn, editProfileBtn, ...]
let images [hmImg, ....]
func selectItemAtIndex(index:Int) {
    buttons.forEach {
      $0.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
   }  
   images.forEach {
       $0.hidden = true 
   }  
   buttons[index].setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
   images[index].hidden = false   
}
@IBAction func onBtnClick(sender: UIButton) {
    let index = sender.tag
    selectItemAtIndex(index - 1) // your tags is 1 based.
}

或者您可以使用不同的方式来减少循环。

let images [hmImg, ....]
var currentBtn: UIButton!
var currentImg: UIImageView!
@IBAction func onBtnClick(sender: UIButton) {
   currentBtn?.setTitleColor(UIColor.blackColor(), forState:UIControlState.Normal)
    sender.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
    currentBtn = sender
    let index = sender.tag - 1 // one based tages
    currentImg?.hidden = true
    currentImg = images[index]
    currentImg.hidden = false
}

关于ios - 将按钮的文本颜色从黑色更改为白色,然后恢复原状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34927128/

相关文章:

ios - 多次要求信任开发商

ios - 避免 UIImageView 在不同设备上拉伸(stretch)

ios - 在 UISearchController 中隐藏搜索栏上的取消按钮

ios - Twitter官方iOS应用如何使用自定义文本更改状态栏

Android 的 addJavascriptInterface 在 iOS 中的等价物

iOS facebookSDK 获取用户详细信息

swift - 分配 backgroundColor 时,我的带有 UIVibrancyEffect 的 UIButton 的标题不可见

ios - 初始化UIButton并将其放置在具有UIPickerView的UIActionSheet中

ios - swift 在代码中把一个生成的imageView变成一个UIButton

ios - 是否支持在 CalDAV 或 ical (icalendar) 中将事件标记为只读