Ios 如何在UI按钮上设置gradientLayer前面的图像和文本?

Ios 如何在UI按钮上设置gradientLayer前面的图像和文本?,ios,swift,uibutton,layer,Ios,Swift,Uibutton,Layer,我的代码如下所示: pickerCountry = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton pickerCountry.frame = CGRectMake(self.view.frame.size.width/2 - pickerCountry.frame.size.width, 50, 100, 100) pickerCountry.center.x = self.view.center.x

我的代码如下所示:

    pickerCountry = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    pickerCountry.frame = CGRectMake(self.view.frame.size.width/2 - pickerCountry.frame.size.width, 50, 100, 100)
    pickerCountry.center.x = self.view.center.x
    pickerCountry.setTitle("Start1", forState: UIControlState.Normal)
    pickerCountry.setImage(UIImage(named: "Britain_mdpi.png"), forState: UIControlState.Normal)
    pickerCountry.tintColor = UIColor.blackColor()
    pickerCountry.titleLabel?.tintColor = UIColor.blackColor()
    pickerCountry.layer.cornerRadius = 2

    pickerCountry.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 200)
    pickerCountry.titleEdgeInsets = UIEdgeInsets(top: 5, left: 50, bottom: 5, right: 5)

    pickerCountry.addTarget(self, action: "countryPicker:", forControlEvents: UIControlEvents.TouchUpInside)
    ContentView.addSubview(pickerCountry)
    let colorTop = UIColor(netHex:0xffffff).CGColor
    let colorBottom = UIColor(netHex:0xebebeb).CGColor

    pickerCountryGradientLayer.masksToBounds = true
    pickerCountryGradientLayer.cornerRadius = pickerCountry.layer.cornerRadius
    pickerCountryGradientLayer.colors = [ colorTop, colorBottom]
    pickerCountryGradientLayer.locations = [ 0.0, 1.0]

    pickerCountry.layer.insertSublayer(pickerCountryGradientLayer, below: pickerCountry.imageView?.layer)
我在a
func
中添加梯度,如下所示:

    pickerCountry = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    pickerCountry.frame = CGRectMake(self.view.frame.size.width/2 - pickerCountry.frame.size.width, 50, 100, 100)
    pickerCountry.center.x = self.view.center.x
    pickerCountry.setTitle("Start1", forState: UIControlState.Normal)
    pickerCountry.setImage(UIImage(named: "Britain_mdpi.png"), forState: UIControlState.Normal)
    pickerCountry.tintColor = UIColor.blackColor()
    pickerCountry.titleLabel?.tintColor = UIColor.blackColor()
    pickerCountry.layer.cornerRadius = 2

    pickerCountry.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 200)
    pickerCountry.titleEdgeInsets = UIEdgeInsets(top: 5, left: 50, bottom: 5, right: 5)

    pickerCountry.addTarget(self, action: "countryPicker:", forControlEvents: UIControlEvents.TouchUpInside)
    ContentView.addSubview(pickerCountry)
    let colorTop = UIColor(netHex:0xffffff).CGColor
    let colorBottom = UIColor(netHex:0xebebeb).CGColor

    pickerCountryGradientLayer.masksToBounds = true
    pickerCountryGradientLayer.cornerRadius = pickerCountry.layer.cornerRadius
    pickerCountryGradientLayer.colors = [ colorTop, colorBottom]
    pickerCountryGradientLayer.locations = [ 0.0, 1.0]

    pickerCountry.layer.insertSublayer(pickerCountryGradientLayer, below: pickerCountry.imageView?.layer)
当我使用最后一行时,
insertSublayer
像这样,我看到了图像,我看到了标题,但它的颜色是白色的,或者有些奇怪。我将颜色设置为黑色,因此如果这真的有效,我如何更改标题颜色


我尝试使用了
addSublayer
,但是我看到的是标题标签,而不是图像。

您提到您已经设置了
ui按钮的标题颜色,但我看不到您在哪里设置了
ui按钮的标题颜色。使用:

pickerCountry.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

我的错,我以为tintColor是标题颜色,它一直工作到现在,谢谢