如果我使用类似 - UIView(frame:..)
AND 这样的框架创建UIView,还会在此视图上设置约束,框架尺寸是否优先于约束? iOS运行时会忽略约束吗?
我在viewDidLoad()
中创建了一个UIView,其框架和附加约束对此视图。但是,根本没有强制执行约束,并且视图将在传递给初始化程序的框架中呈现。
[编辑]
我的代码如下。我试图在覆盖整个屏幕的UIView(previewContainer,配置有故事板上的约束)上以编程方式添加到AVCaptureVideoPreviewLayer(viewPreviewLayer)的叠加层。
...
var videoPreviewLayer:AVCaptureVideoPreviewLayer!
@IBOutlet weak var previewContainer: UIView!
....
override func viewDidLoad() {
.....
self.configurePreviewLayer()
}
override func viewDidLayoutSubviews() {
print("viewDidLayoutSubviews")
super.viewDidLayoutSubviews()
self.videoPreviewLayer.frame = self.previewContainer.bounds
}
func configurePreviewLayer() {
print("configurePreviewLayer()")
self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.previewContainer.layer.addSublayer(self.videoPreviewLayer)
configureBlurView()
}
func configureBlurView() {
print("configureBlurView()")
// Blur View
let viewFinderRadius:CGFloat = self.view.bounds.width / 2.0 - 16.0
print("configureBlurView:\(self.previewContainer.bounds.size.height)")
let blurView = createOverlay(frame:self.previewContainer.bounds,xOffset: self.view.bounds.size.width / 2.0, yOffset: self.view.bounds.size.height / 2.0, radius: viewFinderRadius)
blurView.translatesAutoresizingMaskIntoConstraints = false
self.previewContainer.addSubview(blurView)
self.blurView = blurView
blurView.leftAnchor.constraint(equalTo: self.previewContainer.leftAnchor).isActive = true
blurView.topAnchor.constraint(equalTo:self.previewContainer.topAnchor).isActive = true
blurView.rightAnchor.constraint(equalTo:self.previewContainer.rightAnchor).isActive = true
blurView.bottomAnchor.constraint(equalTo:self.previewContainer.bottomAnchor).isActive = true
}
func createOverlay(frame : CGRect, xOffset: CGFloat, yOffset: CGFloat, radius: CGFloat) -> UIView
{
let overlayView = UIView(frame: frame)
overlayView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
let path = CGMutablePath()
path.addArc(center: CGPoint(x: xOffset, y: yOffset), radius: radius, startAngle: 0.0, endAngle: 2.0 * CGFloat.pi, clockwise: false)
path.addRect(CGRect(origin: .zero, size: overlayView.frame.size))
let maskLayer = CAShapeLayer()
maskLayer.backgroundColor = UIColor.black.cgColor
maskLayer.path = path
maskLayer.fillRule = kCAFillRuleEvenOdd
overlayView.layer.mask = maskLayer
overlayView.clipsToBounds = true
return overlayView
}
以下是渲染屏幕的外观(正如您所看到的,叠加层不会覆盖整个屏幕,换句话说就是覆盖预览容器的整个边界) -
[更新]
我可能找到了罪魁祸首。正是这段代码掩盖了叠加层 -
let path = CGMutablePath()
path.addArc(center: CGPoint(x: xOffset, y: yOffset), radius: radius, startAngle: 0.0, endAngle: 2.0 * CGFloat.pi, clockwise: false)
path.addRect(CGRect(origin: .zero, size: overlayView.frame.size))
let maskLayer = CAShapeLayer()
maskLayer.backgroundColor = UIColor.black.cgColor
maskLayer.path = path
maskLayer.fillRule = kCAFillRuleEvenOdd
overlayView.layer.mask = maskLayer
如果我删除上面的面具,一切都会正确呈现。现在我需要弄清楚如何绘制该遮罩(相机的取景器)。
答案 0 :(得分:0)
iOS运行时会忽略约束吗?
如果正确设置了约束,则不会:忽略frame
。但是,你没有表现出你所做的,所以不可能说出你所看到的和为什么。