gpt4 book ai didi

ios - 如何在UIButton 的gradientLayer 前面设置图片和文字?

转载 作者:行者123 更新时间:2023-11-28 07:06:35 26 4
gpt4 key购买 nike

我的代码是这样的:

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

最佳答案

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

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

关于ios - 如何在UIButton 的gradientLayer 前面设置图片和文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com