【问题标题】:Set UIButton text color programmatically [duplicate]以编程方式设置 UIButton 文本颜色 [重复]
【发布时间】:2015-12-16 19:13:27
【问题描述】:

我想更改表格视图页脚中的按钮文本颜色。这是我正在使用的代码,但它不起作用

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
    footerView.backgroundColor = UIColor.blackColor()

    let rgbValue = 0x4097d4
    let color = Util.getColor(rgbValue)




    let button   = UIButton(type: UIButtonType.System) as UIButton
    button.frame = CGRectMake(0, 0, 414, 65)

    button.setTitle("my Button", forState: UIControlState.Normal)
    button.titleLabel?.textColor = UIColor.whiteColor()
    button.titleLabel?.font = UIFont(name: "Montserrat-Regular", size: 20.0)



     button.backgroundColor =  UIColor( red: CGFloat(color[0]), green: CGFloat(color[1]), blue:CGFloat(color[2]), alpha: 1.0 )



    footerView.addSubview(button)


    return footerView
}

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40.0
}

一个问题是文本颜色没有改变,另一个问题是我的表格视图中有很多静态行。我正在修复底部的按钮,但问题是当屏幕到达末尾时,如果我点击屏幕并将屏幕拖到上方,按钮后仍会显示一些空白

【问题讨论】:

  • 尝试过任何其他类型的UIButtonType

标签: ios swift uibutton


【解决方案1】:

不要直接设置标签的颜色;使用-setTitleColor:forState:

button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)

【讨论】:

  • 非常感谢.. 它有效
  • 你能回答我的另一个问题吗?如果可能的话,你能给我一些关于我下一个问题的小提示
  • 对于另一个问题,这取决于你想要什么行为。如果您根本不希望页脚随表格视图滚动,请参阅this question。如果您确实希望它滚动,但希望按钮的颜色超出按钮的底部,那么您应该在按钮下方创建一个额外的纯色视图来填充该空间。
  • 感谢您的回答。好吧,我解决了这个问题。实际上这是我应该取消选中的反弹属性,现在我做了
  • 非常感谢...挽救了这一天...我之前尝试过 button.titleLabel?.textColor = .red 并且当从单元格上的笔尖唤醒时它不起作用...但是您的方法按预期工作..
猜你喜欢
  • 2017-01-02
  • 2014-06-24
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
相关资源
最近更新 更多