【问题标题】:How to set image and text in front of gradientLayer on UIButton?UIButton如何在gradientLayer前面设置图片和文字?
【发布时间】:2015-05-19 22:18:51
【问题描述】:

我的代码如下所示:

    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)

我在 func 中添加渐变,如下所示:

    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,但我看到的是标题标签而不是图片。

【问题讨论】:

    标签: ios swift uibutton layer


    【解决方案1】:

    您提到您已设置 UIButton 的标题颜色,但我看不到您在哪里设置 UIButton 的标题颜色。使用:

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

    【讨论】:

    • 我的错,我以为 tintColor 是标题颜色,到现在还有效,谢谢
    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 2011-06-23
    • 2012-07-27
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2020-07-07
    • 2013-03-23
    相关资源
    最近更新 更多