【问题标题】:How to make a gradient button in the footer of UICollectionView in swift?如何快速在 UICollectionView 的页脚中制作渐变按钮?
【发布时间】:2019-10-24 20:54:31
【问题描述】:

我遇到了一个问题。我正在制作一个应用程序,我需要在 UICollectionView 的页脚内制作一个带有渐变颜色的按钮,问题是我无法通过情节提要制作它,所以我必须以编程方式制作它UICollectionView 的页脚内。但是不知道怎么弄。

事情是我已经尝试过,我已经完成了UICollectionView的Footer中UIButton的基本结构。

case UICollectionView.elementKindSectionFooter:

    let footer = outerMainCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath) as! headerReusableView

    let button = UIButton(frame: CGRect(x: 0, y: 0, width: collectionView.frame.width - 50, height: 40))

    if isGuest {
        button.backgroundColor = .red
    } else {
        button.backgroundColor = .black
    }

    button.setTitle("ALL", for: .normal)
    button.setTitleColor(COLORWHITE, for: .normal)
    button.addTarget(self, action: #selector(footerButton), for: .touchUpInside)

    footer.addSubview(button)

    footer.backgroundColor = UIColor.green

    return footer

我想让它在浅橙色和深橙色之间渐变,例如使用任何十六进制值,并且我想让它的中心高度为 40,并从所有侧面设置边距 - 顶部、底部、左侧、右侧。

【问题讨论】:

  • 这段代码放在哪里?告诉我们headerReusableView
  • headerReusableView 与我的问题无关,所以我没有在这里写下来。我已经得到了我的问题的答案。顺便说一句,谢谢你的反对票。 :-)
  • 我问是因为看起来你每次出列页脚时都会添加一个按钮,这非常糟糕;顺便说一句,我没有投反对票-.-

标签: ios swift uicollectionview uibutton


【解决方案1】:

集合视图本身提供了使用 Xib 和情节提要添加页脚的选项,但如果您想以编程方式添加约束,您可以使用以下函数来完成

func addSubviewInSuperview(subview : UIView , andSuperview superview : UIView) {

    subview.translatesAutoresizingMaskIntoConstraints = false;

    let leadingConstraint = NSLayoutConstraint(item: subview,
                                               attribute: .leading,
                                               relatedBy: .equal,
                                               toItem: superview,
                                               attribute: .leading,
                                               multiplier: 1.0,
                                               constant: 0)

    let trailingConstraint = NSLayoutConstraint(item: subview,
                                                attribute: .trailing,
                                                relatedBy: .equal,
                                                toItem: superview,
                                                attribute: .trailing,
                                                multiplier: 1.0,
                                                constant: 0)

    let topConstraint = NSLayoutConstraint(item: subview,
                                           attribute: .top,
                                           relatedBy: .equal,
                                           toItem: superview,
                                           attribute: .top,
                                           multiplier: 1.0,
                                           constant: 0)

    let bottomConstraint = NSLayoutConstraint(item: subview,
                                              attribute: .bottom,
                                              relatedBy: .equal,
                                              toItem: superview,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 0.0)

    superview.addSubview(subview)
    superview.addConstraints([leadingConstraint,trailingConstraint,topConstraint,bottomConstraint])
}  

将渐变设置为按钮背景:-

func setGradient() {
    let color1 =  UIColor.red.cgColor
    let color2 = UIColor.blue.cgColor

    let gradientLayer = CAGradientLayer()
    gradientLayer.colors = [color1, color2]
    gradientLayer.locations = [0.0, 1.0]
    gradientLayer.frame = self.view.bounds

    self.btn.layer.insertSublayer(gradientLayer, at:0)
}

【讨论】:

  • 非常感谢您的帮助。我根据我的轻松对您的代码进行了修改,现在它工作得很好。荣誉。
【解决方案2】:

我有一个扩展程序可以帮助您在UIButtonUIView 中添加渐变色。

扩展

extension UIView {
    func applyGradient(colours: [UIColor]) -> Void {
        self.applyGradient(colours, locations: nil)
    }

    func applyGradient(colours: [UIColor], locations: [NSNumber]?) -> Void {
        let gradient: CAGradientLayer = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.colors = colours.map { $0.CGColor }
        gradient.locations = locations
        self.layer.insertSublayer(gradient, atIndex: 0)
    }
}

您可以使用extension,如下所示。确保在ViewController 中声明全局按钮属性,如下例所示。

class ViewController: UIViewController {

    @IBOutlet weak var yourButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.yourButton.applyGradient([UIColor.yellowColor(), UIColor.blueColor()])
    }
}

【讨论】:

  • 感谢您的帮助,但主要问题是按钮本身,我如何使其居中并以编程方式从各个方面放置边距,我通常在情节提要上进行设计,但为此,我需要程序化方法。谢谢...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
相关资源
最近更新 更多