Ios 如何在swift中的UICollectionView的页脚中创建渐变按钮?
我遇到了一个问题。我正在制作一个应用程序,我需要在UICollectionView的页脚内制作一个带有渐变颜色的按钮,问题是我无法通过故事板制作,所以我必须在UICollectionView的页脚内以编程方式制作。但我不知道怎么做 问题是我已经尝试过了,我已经完成了在UICollectionView的页脚中创建UIButton的基本结构Ios 如何在swift中的UICollectionView的页脚中创建渐变按钮?,ios,swift,uicollectionview,uibutton,Ios,Swift,Uicollectionview,Uibutton,我遇到了一个问题。我正在制作一个应用程序,我需要在UICollectionView的页脚内制作一个带有渐变颜色的按钮,问题是我无法通过故事板制作,所以我必须在UICollectionView的页脚内以编程方式制作。但我不知道怎么做 问题是我已经尝试过了,我已经完成了在UICollectionView的页脚中创建UIButton的基本结构 case UICollectionView.elementKindSectionFooter: let footer = outerMainColle
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,并且从四面八方(顶部、底部、左侧、右侧)设置边距。我有一个扩展,它将帮助您在
UIButton
和UIView
中添加渐变颜色
扩展
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)
}
}
您可以像下面那样使用扩展名。确保在ViewController
中声明按钮属性为全局,如下例所示
class ViewController: UIViewController {
@IBOutlet weak var yourButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.yourButton.applyGradient([UIColor.yellowColor(), UIColor.blueColor()])
}
}
集合视图本身提供了使用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)
}
我很感谢你的帮助,但主要的问题是按钮本身,我如何使它居中并以编程方式从各个方面设置边距,我通常在故事板上进行设计,但为此,我需要编程方法。谢谢…这段代码放在哪里?向我们展示headerReusableView
classheaderReusableView与我的问题无关,所以我没有把它写在这里。我的问题已经有了答案。顺便说一句,谢谢你投了反对票。:-)我这样问是因为每次你把一个页脚排出来的时候都会添加一个按钮,这很糟糕;顺便说一句,我没有投反对票-非常感谢你的帮助。我根据自己的意愿修改了你的代码,现在它运行得非常好。荣誉