ios - CALayer 不使用自动布局调整大小

标签 ios

我通过创建一个渐变层创建了一个在 tableView 中使用的进度条。它工作得很好。

iPhone5: enter image description here

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

iPhone6: enter image description here

我觉得这非常愚蠢,但没关系。几个月来,我环顾四周,试图了解如何解决这个问题,但我没有找到答案。 任何人是否知道如何使用 UIView 调整 CALayers 的大小?任何帮助将不胜感激!谢谢。

        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 的默认图层会随其 View 调整大小,但子图层不会(如您所见)。实现这项工作的一种方法是创建一个自定义 View 类,将问题中的代码移至其中,并覆盖 layoutSublayersOfLayer ,您可以在其中将渐变层设置为与 View 大小相同。因为此代码现在位于自定义类中,所以我还创建了一个属性 percentageCompleted(而不是局部变量),并添加了一个 willSet 子句,以便在您更改 percentageCompleted 属性时随时更新栏的外观。

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 中,您可以将 View 的类更改为 RDProgressView(在我的示例中),而在 cellForRowAtIndexPath 中,您只需要获取对 View 的引用,并设置其 percentageCompleted 属性。

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

关于ios - CALayer 不使用自动布局调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111099/

相关文章:

ios - Swift UIAlertAction segue 数据传递

iphone - 引用计数的不正确减少

ios - XCode CI 在运行 UITests 时卡住了(正在为 -[AppTests textExample] 上传 10 个屏幕截图)

ios - AudioUnitRender Error -50 含义

ios - UICollectionView 单元可重用性问题 - swift

ios - 在提交到 Apple App Store 之前混淆 Swift 代码

ios - 如何正确处理 Storyboard中具有相似 Controller 的相似 View 而不重复代码?

ios - 如何在处理通知时禁止 OneSignal 打开 WebView

ios - 在 iOS 中使用 Storyboard中的 .xib 文件调用 ViewController 时遇到问题

ios - kCLAuthorizationStatusRestricted 显示在我的一个应用程序中,但同一设备上的另一个应用程序中没有显示