2015-12-16 63 views
1

我想更改我的表格視圖頁腳中的按鈕文字顏色。下面是我使用的代碼,但它不工作以編程方式設置UIButton文本顏色

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 
} 

一個問題是,文本顏色不會改變,另一個問題是,我有很多的靜態行的在我的tableview。我正在固定底部的按鈕,但問題是屏幕到達底部時,如果我點擊屏幕並在上方拖動屏幕,它仍然在按鈕後顯示一些空白空間

+0

嘗試了其他類型的'UIButtonType'? –

+0

@TravisGriggs nope – hellosheikh

回答

5

不要直接設置標籤的顏色;使用-setTitleColor:forState:

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

非常感謝你..它的作品 – hellosheikh

+0

你能回答我的另一個問題。如果可能的話,你可以給我關於我的下一個問題的小提示 – hellosheikh

+0

對於另一個問題,這取決於你想要的行爲。如果您不希望頁腳根本不需要滾動表格視圖,那麼請參考[這個問題](http://stackoverflow.com/questions/15687370/)。如果您確實希望它滾動,但希望按鈕的顏色超出按鈕的底部,則應該在按鈕下方創建一個附加的純色視圖以填充該空間。 –

相關問題