Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios iPad上的iPhone渐变背景尺寸_Ios_Swift_Xcode_Ios Simulator - Fatal编程技术网

Ios iPad上的iPhone渐变背景尺寸

Ios iPad上的iPhone渐变背景尺寸,ios,swift,xcode,ios-simulator,Ios,Swift,Xcode,Ios Simulator,我在iPad上设置渐变背景时遇到问题。在iPhone上一切正常,但当我使用iPad时,渐变背景有iPhone尺寸 我用来制作渐变的代码如下所示 func setGradientToTableView(tableView: UITableView) { let gradientBackgroundColors = [UIColor(red: 190.0/255.0, green: 219.0/255.0, blue: 0.0/255.0, alpha: 1).cgColor,

我在iPad上设置渐变背景时遇到问题。在iPhone上一切正常,但当我使用iPad时,渐变背景有iPhone尺寸

我用来制作渐变的代码如下所示

func setGradientToTableView(tableView: UITableView) {

        let gradientBackgroundColors = [UIColor(red: 190.0/255.0, green: 219.0/255.0, blue: 0.0/255.0, alpha: 1).cgColor, UIColor(red: 13.0/255.0, green: 227.0/255.0, blue: 97.0/255.0, alpha: 1).cgColor]

        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientBackgroundColors
        gradientLayer.startPoint = CGPoint(x: 0,y: 0)

        gradientLayer.frame = tableView.bounds
        let backgroundView = UIView(frame: tableView.bounds)
        backgroundView.layer.insertSublayer(gradientLayer, at: 0)
        tableView.backgroundView = backgroundView
    }

我猜你没有使用自动布局


使用iPad模拟器启动应用程序时,请检查tableviewsize。我认为它没有您想象的那么大。

您需要在
viewdilayoutsubviews
中为层设置框架:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.backgroundView?.layer.sublayers?.forEach { $0.frame = tableView.bounds }
    }

此外,正如@Jan Schlorf在评论中所建议的,您可以将图层存储为属性:

    lazy var gradientLayer = CAGradientLayer()

    //...

    func setGradientToTableView(tableView: UITableView) {
        let gradientBackgroundColors = [UIColor(red: 190.0/255.0, green: 219.0/255.0, blue: 0.0/255.0, alpha: 1).cgColor, UIColor(red: 13.0/255.0, green: 227.0/255.0, blue: 97.0/255.0, alpha: 1).cgColor]

        gradientLayer.colors = gradientBackgroundColors
        gradientLayer.startPoint = CGPoint(x: 0,y: 0)

        gradientLayer.frame = tableView.bounds
        let backgroundView = UIView(frame: tableView.bounds)
        backgroundView.layer.insertSublayer(gradientLayer, at: 0)
        tableView.backgroundView = backgroundView
    }

    //...

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        gradientLayer.frame = tableView.bounds
    }


您在哪里调用
setGradientToTableView()
?@MumtazHussain在
viewDidLoad()中。我也有同样的问题与自定义视图。总是在左上角。我用了自动布局。查看单元格大小。建议改进的单元格大小;我会将
gradientLayer
存储在一个属性中,并且只更改其框架,因为您的代码可能不仅仅调整
gradientLayer
的大小。