【发布时间】:2018-01-30 12:03:22
【问题描述】:
在我的应用程序中,我将渐变层添加到 UIView 和 UIToolBar,但它并没有完全填充视图
let gradient:CAGradientLayer = CAGradientLayer()
gradient.frame = self.vw_gradientForToolBar.bounds
gradient.colors = [hexStringToUIColor(hex: "#5d8f32").cgColor,hexStringToUIColor(hex: "#04667f").cgColor]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 0.5, y: 0)
UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0.0)
let img : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.toolBar.setBackgroundImage(img, forToolbarPosition: .any, barMetrics: .default)
vw_gradientForToolBar.layer.addSublayer(gradient)
查看层次结构
在此处输入图片说明
【问题讨论】:
-
检查您的视图层次结构必须在您的视图上方有一个 UIView(那个白色矩形),检查您是否不小心添加了一个视图
-
不,没有添加 UIView。我添加了 UIView 约束,它将等于设备或超级视图的宽度。它是最顶级的视图
-
你能发一张你的视图层次的截图吗?
-
添加了视图层次
-
我的意思是“Debug View Hierarchy”截图,当你的应用运行时,对不起
标签: ios swift4 cagradientlayer