自定义地物未显示在“今日扩展"中 [英] Custom figure doesn't display in Today Extension

查看:127
本文介绍了自定义地物未显示在“今日扩展"中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在今天的扩展中取得进展.我为此创建了其他类.我可以在IBDesignable的帮助下在情节提要中看到它.但是,圆圈​​并没有出现在实际设备(iPhone 5s,iPad 3)或模拟器上的今天扩展中.我该如何解决?

I make progress circle in today extension. I create additional class for it. I can see it in storyboard with help of IBDesignable. But circle doesn't appear in today extension on real device ( iPhone 5s, iPad 3) or simulator. How can I fix it?

    import UIKit

@IBDesignable
class CPUView: UIView {

    // MARK: - colors
    @IBInspectable var firstColor: UIColor = UIColor.blackColor()
    @IBInspectable var secondColor: UIColor = UIColor.greenColor()
    @IBInspectable var thirdColor: UIColor = UIColor.yellowColor()

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        self.addCircle(10, capRadius: 0.0)
    }

    func addOval(lineWidth: CGFloat, path: CGPath, strokeColor: UIColor, fillColor: UIColor, strokeStart: CGFloat, strokeEnd: CGFloat) {
        let shape = CAShapeLayer()
        shape.lineWidth = lineWidth
        shape.path = path
        shape.strokeColor = strokeColor.CGColor
        shape.fillColor = fillColor.CGColor
        shape.strokeStart = strokeStart
        shape.strokeEnd = strokeEnd
        layer.addSublayer(shape)
    }

    func addCircle(arcRadius: CGFloat, capRadius: CGFloat) {
        let X = CGRectGetMidX(self.bounds)
        let Y = CGRectGetMidY(self.bounds)

        let firstPathCircle = UIBezierPath(ovalInRect: CGRectMake(X - arcRadius/2, Y - arcRadius/2, arcRadius, arcRadius)).CGPath

        self.addOval(0.1, path: firstPathCircle, strokeColor: UIColor.redColor(), fillColor: UIColor.yellowColor(), strokeStart: 0.0, strokeEnd: 0.5)
    }

}

更新

我试图用drawRect编写代码,但是得到了相同的结果

I tried to write the code in drawRect but I got the same result

 override func drawRect(rect: CGRect) {
    // Drawing code
    let x = CGRectGetMidX(self.bounds)
    let y = CGRectGetMidY(self.bounds)
    let radius = CGFloat()

    let path = UIBezierPath(ovalInRect: CGRectMake(x - radius/2, y - radius, 100, 100)).CGPath

    let shape = CAShapeLayer()
    shape.lineWidth = 0.0
    shape.fillColor = UIColor.greenColor().CGColor
    shape.path = path
    layer.addSublayer(shape)
}

推荐答案

我写了以下代码,它对我有用

I wrote the following code and it works for me

override func viewDidLoad() {
        super.viewDidLoad()
        self.preferredContentSize = CGSize(width: self.view.frame.width, height: 200.0)
    }

这篇关于自定义地物未显示在“今日扩展"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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