CALayer没有使用Autolayout调整大小 [英] CALayer not resizing with Autolayout

查看:204
本文介绍了CALayer没有使用Autolayout调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过创建渐变图层创建了一个用于tableView的进度条。它完美地运作。

I have created a progress bar to be used in a tableView by creating a gradient layer. It works perfectly.

iPhone5:

为了在多个设备上使用该应用程序,我在Storyboard中创建了UIView,标记了它并添加了约束。
但是,当我在iPhone 6上使用该应用程序时,CALayer不会调整大小。

In order to use the app on multiple devices, I have created the UIView in Storyboard, tagged it and added constraints. However, when I use the app on an iPhone 6 the CALayer don't resize.

iPhone6:

我发现这非常愚蠢,但没关系。我环顾四周,试图了解如何解决这个问题几个月,但我做得很短。 ANYONE 是否知道如何使用UIView调整CALayers的大小?任何帮助将非常感谢!谢谢。

I find this extremely stupid, but never mind. I have looked around and tried to understand how to solve this for months, but I have come up short. Does ANYONE know how to make CALayers resize with the UIView? Any help would be very much appreciated ! Thank you.

        progressBar =  cell.contentView.viewWithTag(3) as UIView!
        progressBar.layer.cornerRadius = 4
        progressBar.layer.masksToBounds = true


        // create gradient layer
        let gradient : CAGradientLayer = CAGradientLayer()

        // create color array
        let arrayColors: [AnyObject] = [
            UIColor (red: 255/255, green: 138/255, blue: 1/255, alpha: 1).CGColor,
            UIColor (red: 110/255, green: 110/255, blue: 118/255, alpha: 1).CGColor]

        // set gradient frame bounds to match progressBar bounds
        gradient.frame = progressBar.bounds

        // set gradient's color array
        gradient.colors = arrayColors

        //Set progress(progressBar)
        var percentageCompleted = 0.6

        gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient.locations = [percentageCompleted, percentageCompleted]
        gradient.endPoint = CGPoint(x: 1, y: 0.5)

        // replace base layer with gradient layer
        progressBar.layer.insertSublayer(gradient, atIndex: 0)


推荐答案

UIView的默认图层会根据其视图调整大小,但子图层不会(如您所知)。实现此功能的一种方法是创建自定义视图类,将问题中的代码移动到其中,然后覆盖layoutSublayersOfLayer,您可以将渐变图层设置为与视图相同的大小。因为此代码现在位于自定义类中,所以我还创建了一个属性percentageCompleted(而不是局部变量),并添加了一个willSet子句,以便在更改percentageCompleted属性时更新条的外观。

The default layer of a UIView does resize with its view, but sublayers don't (as you found out). One way to make this work is to create a custom view class, move the code you have in your question to it, and override layoutSublayersOfLayer where you can set the gradient layer to be the same size as the view. Because this code is now in a custom class, I also created a property percentageCompleted (instead of a local variable), and added a willSet clause so the bar's appearance is updated any time you change the percentageCompleted property.

class RDProgressView: UIView {

    private let gradient : CAGradientLayer = CAGradientLayer()

    var percentageCompleted: Double = 0.0 {
        willSet{
            gradient.locations = [newValue, newValue]
        }
    }

    override func awakeFromNib() {
        self.layer.cornerRadius = 4
        self.layer.masksToBounds = true

        // create color array
        let arrayColors: [AnyObject] = [
            UIColor (red: 255/255, green: 138/255, blue: 1/255, alpha: 1).CGColor,
            UIColor (red: 110/255, green: 110/255, blue: 118/255, alpha: 1).CGColor]

        // set gradient's color array
        gradient.colors = arrayColors

        //Set progress(progressBar)
        gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient.locations = [percentageCompleted, percentageCompleted]
        gradient.endPoint = CGPoint(x: 1, y: 0.5)

        self.layer.insertSublayer(gradient, atIndex: 0)
    }

    override func layoutSublayersOfLayer(layer: CALayer!) {
        super.layoutSublayersOfLayer(layer)
        gradient.frame = self.bounds
    }
}

在IB中,您可以将视图的类更改为RDProgressView(在我的示例中),在cellForRowAtIndexPath中,您只需要获取对视图的引用,并且设置其percentageCompleted属性。

In IB, you would change the class of your view to RDProgressView (in my example), and in cellForRowAtIndexPath, you would only need to get a reference to the view, and set its percentageCompleted property.

progressBar =  cell.contentView.viewWithTag(3) as RDProgressView! 
progressBar.percentageCompleted = 0.2

这篇关于CALayer没有使用Autolayout调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆