UITable视图控制器上的渐变背景

时间:2018-10-13 11:57:57

标签: ios swift xcode

如何在UITable View Controller上设置渐变背景?

如果我执行上述代码,则渐变色效果很好,但是我无法在屏幕上查看UIImage视图和UIButton。我只能看到渐变色..请帮助... < / p>

import UIKit

class HomeTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setGradientBackground()

    }

    //Mark:- Gradiant background colour

    func setGradientBackground() {


        let topColor = UIColor(red: 19/255, green: 163/255, blue: 203/255, alpha: 1)
        let bottomColor = UIColor(red: 131/255, green: 197/255, blue: 86/255, alpha: 1)

        let gradientColors: [CGColor] = [topColor.cgColor, bottomColor.cgColor]
        let gradientLocations: [Float] = [0.0, 0.5]
        let gradientLayer: CAGradientLayer = CAGradientLayer()

        gradientLayer.colors = gradientColors
        gradientLayer.locations = gradientLocations as [NSNumber]?

        gradientLayer.frame = self.view.bounds
        self.view.layer.insertSublayer(gradientLayer, at: 0)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}

1 个答案:

答案 0 :(得分:0)

尝试设置backgroundView中的tableView。初始化一个Uiview渐变视图,使其绑定等于tableView,执行所有必要的操作以设置渐变,并将此UIview设置为tableView的背景视图。