我正在尝试"resizeLayer"
通过对UIView selectedShape
进行子分层来将其添加到UIView中selectedShape
let sublayer = CALayer() sublayer.bounds = selectedShape.bounds //even when inserted this line, sublayer still doesn't show up sublayer.frame = selectedShape.frame sublayer.contents = UIImage(named: "resizeLayer") selectedShape?.layer.addSublayer(sublayer)
但是当我运行代码时,我根本看不到该层
我什至尝试"resizeLayer"
通过UIView来查看图像"selectedShape"
let resizeFrame = UIImageView(image: UIImage(named: "resizeLayer")) resizeFrame.frame = selectedShape.frame resizeFrame.contentMode = UIViewContentMode.ScaleAspectFill selectedShape.addSubview(resizeFrame)
但是,仍然"resizeLayer"
没有出现!
仅当我"resizeLayer"
在整体视图中添加时,它才会显示:
let resizeFrame = UIImageView(image: UIImage(named: "resizeLayer")) resizeFrame.frame = selectedShape.frame resizeFrame.contentMode = UIViewContentMode.ScaleAspectFill selectedShape.addSubview(resizeFrame) self.view.insertSubview(resizeFrame, aboveSubview: selectedShape) //add this line
任何帮助,将不胜感激!
如果相关,这就是我所做的 selectedShape
selectedShape = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60)) selectedShape.layer.cornerRadius = 10 selectedShape.backgroundColor = UIColor.blueColor() canvas.addSubview(selectedShape) //canvas is the view I'm adding selectedShape to
这是"resizeLayer"
我要添加的图像
蓝色正方形是selectedShape
。如您所见,该图层未显示。
我想发生什么
您正在使用整个框架selectedShape
。您应该只使用width
and和height
因为x
并且y
应该为零。图像被添加到selectedShape
该点,因此该点(0,0)
位于selectedShape
视图的左上方。
resizeFrame.frame = CGRect(origin: CGPointZero, size: selectedShape.frame.size)
我承认,这让我难过的时间比原本应该的更长。