Swift - UIView frame is not equal to UIView.layer.frame

I want to draw a UIView layer, but when I do this, the layer frame is not (In Preview) for the UIView frame. enter image description here

class ViewController: UIViewController {

    var graphHeight:CGFloat = 100
    var graphSize:CGFloat!

    override func viewDidLoad() {
        super.viewDidLoad()
        graphSize = self.view.frame.height/CGFloat(M_PI)
        let graphRect:CGRect = CGRectMake(0, graphHeight, self.view.frame.width, graphSize)
        let background = blueGardient()
        var theView:UIView = UIView(frame: graphRect)
        background.frame = theView.frame
        theView.backgroundColor = UIColor.yellowColor()
        theView.layer.cornerRadius = 8
        theView.layer.borderWidth = 1
        theView.layer.borderColor = UIColor.redColor().CGColor
        theView.layer.insertSublayer(background, atIndex: 0)
        self.view.addSubview(theView)

    }

    func blueGardient()->CAGradientLayer{
        let topColor = UIColor(red: 0, green: 0, blue: 255, alpha: 0.7)
        let bottomColor = UIColor(red: 0, green: 0, blue: 255, alpha: 0.9)
        let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
        let gradientLocations: [Float] = [0.0, 1.0]
        let gradientLayer: CAGradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientColors
        gradientLayer.locations = gradientLocations
        return gradientLayer
    }
}

Frame is equal

(0.0,100.0,320.0,180.800015352393)
(0.0,100.0,320.0,180.800015352393)

but not shown eauql. I try with theView.layer.frame, but to no avail ...

+4
source share
1 answer

, . (theView.frame) , (0,100) , 100 . (background.frame) , (theView), (0,100) , 100 .

frame , bounds a (0,0) origin:

background.frame = CGRect(origin: CGPointZero, size: theView.bounds.size)
+9

All Articles