2015-10-09 72 views
10

我試圖在按鈕上顯示一些文本和圖像。我使用的代碼here在UIButton中添加右鍵

let btnSort = UIButton.buttonWithType(UIButtonType.System) as! UIButton 
    btnSort.frame = CGRectMake(2, 74, 140, 26) 
    btnSort.tintColor = UIColor.whiteColor() 
    btnSort.setImage(UIImage(named:"immgg"), forState: UIControlState.Normal) 
    btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: 100,bottom: 6,right: 14) 
    btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: -30,bottom: 0,right: 34) 
    btnSort.setTitle("SORT", forState: UIControlState.Normal) 
    btnSort.layer.borderWidth = 1.0 
    btnSort.layer.borderColor = UIColor.whiteColor().CGColor 
    btnSort.addTarget(self, action: Selector("showSortTbl"), forControlEvents: UIControlEvents.TouchUpInside) 
    self.view.addSubview(btnSort) 

我能在正確的地方看到圖像,但文本沒有出現。我認爲titleEdgeInsets不起作用。

回答

22

我試過的代碼我得到了輸出什麼問題U面臨。

btnSort.backgroundColor = UIColor.redColor() - >設置背景顏色和檢查

let btnSort = UIButton(type: UIButtonType.System) as UIButton! //this is Swift2.0 in this place use your code 
    btnSort.frame = CGRectMake(2, 74, 140, 40) 
    btnSort.tintColor = UIColor.whiteColor() 
    btnSort.setImage(UIImage(named:"youtube16x16.png"), forState: UIControlState.Normal) 
    btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: 100,bottom: 6,right: 14) 
    btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: -30,bottom: 0,right: 34) 
    btnSort.setTitle("SORT", forState: UIControlState.Normal) 
    btnSort.layer.borderWidth = 1.0 
    btnSort.backgroundColor = UIColor.redColor() --> set the background color and check 
    btnSort.layer.borderColor = UIColor.whiteColor().CGColor 
    btnSort.addTarget(self, action: Selector("showSortTbl"), forControlEvents: UIControlEvents.TouchUpInside) 
    self.view.addSubview(btnSort) 

Swift3

let btnSort = UIButton(type: .system) 
    btnSort.frame = CGRect(x: 2, y: 74, width: 140, height: 40) 
    btnSort.tintColor = UIColor.white 
    btnSort.setImage(UIImage(named:"youtube16x16.png"), for: .normal) 
    btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: 100,bottom: 6,right: 14) 
    btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: -30,bottom: 0,right: 34) 
    btnSort.setTitle("SORT", for: .normal) 
    btnSort.layer.borderWidth = 1.0 
    btnSort.backgroundColor = UIColor.red //--> set the background color and check 
    btnSort.layer.borderColor = UIColor.white.cgColor 
    btnSort.addTarget(self, action: #selector(ViewController.showSortTbl), for: UIControlEvents.touchUpInside) 
    self.view.addSubview(btnSort) 

和處理的動作作爲

func showSortTbl() { 
    // do your stuff here 

} 

輸出

enter image description here

+0

說真的,我不知道是什麼問題,但我仍然沒有得到它。我用紅色檢查過。它是否有問題,因爲我正在使用Swift 1.2? – Saty

+0

@Saty - 我只是添加你的按鈕的背景顏色,並檢查我還顯示的答案,汝試過更新的答案 –

+0

當我刪除圖像,我看到的文字,我正在使用 UIButton.buttonWithType(UIButtonType.System)如! UIButton,因爲我使用的是swift 1.2。我認爲這是造成問題 – Saty

0

只設置背景顏色和顯示文本:

let btnSort = UIButton(type: UIButtonType.System) as UIButton // Shift 2.0 

btnSort.backgroundColor = UIColor.yellowColor() 

OR

btnSort.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal) 
0

請檢查下面的代碼。

希望它能爲你工作。

let teamImage: UIButton = UIButton(frame: CGRect(x: 0, y: 75, width: 100, height: 50)) 
let imageTest = UIImage(named: "immgg") 

teamImage.setTitle("HypnotoadTitle", forState: .Normal) 
teamImage.setBackgroundImage(imageTest, forState: .Normal) 
self.view.addSubview(teamImage)