Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift视图约束问题 我为我的相机屏幕创建了一个遮蔽视图,它模糊了所有的东西,除了中间的一个小矩形。我在测试不同屏幕尺寸时遇到问题。我在测试一个11分职业选手和一个8分职业选手。如果我的故事板视图选择了正确的设备,则约束在设备上看起来很好。但是如果我在故事板中切换手机而不进行更改,这是不正确的。我正在以编程方式创建掩码视图。任何帮助都将不胜感激 func setupBlur() { if !UIAccessibility.isReduceTransparencyEnabled { blurView.backgroundColor = .clear let blurEffect = UIBlurEffect(style: .dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = blurView.bounds blurEffectView.translatesAutoresizingMaskIntoConstraints = false blurEffectView.addConstraints(blurView.constraints) blurView.addSubview(blurEffectView) let maskView = UIView(frame: blurView.bounds) maskView.translatesAutoresizingMaskIntoConstraints = false maskView.addConstraints(blurView.constraints) maskView.backgroundColor = .clear let outerbezierPath = UIBezierPath.init(rect: blurView.bounds) let croppedOriginX = (blurView.bounds.width / 2) - (captureView.bounds.width / 2) let croppedOriginY = (blurView.bounds.height / 2) - (captureView.bounds.height / 2) let frame = CGRect(x: croppedOriginX, y: croppedOriginY, width: captureView.bounds.width, height: captureView.bounds.height) let innerRectPath = UIBezierPath.init(rect: frame) outerbezierPath.append(innerRectPath) outerbezierPath.usesEvenOddFillRule = true let fillLayer = CAShapeLayer() fillLayer.fillRule = kCAFillRuleEvenOdd fillLayer.fillColor = UIColor.green.cgColor // any opaque color would work fillLayer.path = outerbezierPath.cgPath maskView.layer.addSublayer(fillLayer) blurView.mask = maskView; } }_Ios_Swift_Xcode_User Interface_Constraints - Fatal编程技术网

Ios Swift视图约束问题 我为我的相机屏幕创建了一个遮蔽视图,它模糊了所有的东西,除了中间的一个小矩形。我在测试不同屏幕尺寸时遇到问题。我在测试一个11分职业选手和一个8分职业选手。如果我的故事板视图选择了正确的设备,则约束在设备上看起来很好。但是如果我在故事板中切换手机而不进行更改,这是不正确的。我正在以编程方式创建掩码视图。任何帮助都将不胜感激 func setupBlur() { if !UIAccessibility.isReduceTransparencyEnabled { blurView.backgroundColor = .clear let blurEffect = UIBlurEffect(style: .dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = blurView.bounds blurEffectView.translatesAutoresizingMaskIntoConstraints = false blurEffectView.addConstraints(blurView.constraints) blurView.addSubview(blurEffectView) let maskView = UIView(frame: blurView.bounds) maskView.translatesAutoresizingMaskIntoConstraints = false maskView.addConstraints(blurView.constraints) maskView.backgroundColor = .clear let outerbezierPath = UIBezierPath.init(rect: blurView.bounds) let croppedOriginX = (blurView.bounds.width / 2) - (captureView.bounds.width / 2) let croppedOriginY = (blurView.bounds.height / 2) - (captureView.bounds.height / 2) let frame = CGRect(x: croppedOriginX, y: croppedOriginY, width: captureView.bounds.width, height: captureView.bounds.height) let innerRectPath = UIBezierPath.init(rect: frame) outerbezierPath.append(innerRectPath) outerbezierPath.usesEvenOddFillRule = true let fillLayer = CAShapeLayer() fillLayer.fillRule = kCAFillRuleEvenOdd fillLayer.fillColor = UIColor.green.cgColor // any opaque color would work fillLayer.path = outerbezierPath.cgPath maskView.layer.addSublayer(fillLayer) blurView.mask = maskView; } }

Ios Swift视图约束问题 我为我的相机屏幕创建了一个遮蔽视图,它模糊了所有的东西,除了中间的一个小矩形。我在测试不同屏幕尺寸时遇到问题。我在测试一个11分职业选手和一个8分职业选手。如果我的故事板视图选择了正确的设备,则约束在设备上看起来很好。但是如果我在故事板中切换手机而不进行更改,这是不正确的。我正在以编程方式创建掩码视图。任何帮助都将不胜感激 func setupBlur() { if !UIAccessibility.isReduceTransparencyEnabled { blurView.backgroundColor = .clear let blurEffect = UIBlurEffect(style: .dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = blurView.bounds blurEffectView.translatesAutoresizingMaskIntoConstraints = false blurEffectView.addConstraints(blurView.constraints) blurView.addSubview(blurEffectView) let maskView = UIView(frame: blurView.bounds) maskView.translatesAutoresizingMaskIntoConstraints = false maskView.addConstraints(blurView.constraints) maskView.backgroundColor = .clear let outerbezierPath = UIBezierPath.init(rect: blurView.bounds) let croppedOriginX = (blurView.bounds.width / 2) - (captureView.bounds.width / 2) let croppedOriginY = (blurView.bounds.height / 2) - (captureView.bounds.height / 2) let frame = CGRect(x: croppedOriginX, y: croppedOriginY, width: captureView.bounds.width, height: captureView.bounds.height) let innerRectPath = UIBezierPath.init(rect: frame) outerbezierPath.append(innerRectPath) outerbezierPath.usesEvenOddFillRule = true let fillLayer = CAShapeLayer() fillLayer.fillRule = kCAFillRuleEvenOdd fillLayer.fillColor = UIColor.green.cgColor // any opaque color would work fillLayer.path = outerbezierPath.cgPath maskView.layer.addSublayer(fillLayer) blurView.mask = maskView; } },ios,swift,xcode,user-interface,constraints,Ios,Swift,Xcode,User Interface,Constraints,发生这种情况的原因是,当您从viewDidLoad调用setupBlur时,在开始时,blurView.bounds值来自您正在使用的stroyboard,并且您正在使用这些边界设置blurEffectView.frame,然后再根据设备的屏幕进行调整 这不是最好的解决办法,但我希望它能解决你的问题 试试这个: override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.asyncAfter(deadl

发生这种情况的原因是,当您从viewDidLoad调用setupBlur时,在开始时,blurView.bounds值来自您正在使用的stroyboard,并且您正在使用这些边界设置blurEffectView.frame,然后再根据设备的屏幕进行调整

这不是最好的解决办法,但我希望它能解决你的问题

试试这个:

override func viewDidLoad() {
    super.viewDidLoad()
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        self.setupBlur()
    }
}
在我的例子中,我尝试了viewDidLayoutSubviews而不是DispatchQue,但没有成功。然而,viewDidLoad为我工作

override func viewDidAppear() {
    self.setupBlur()
}

添加了blurView后,请尝试调用view.layoutIfNeeded;您正在尝试通过maskView.addConstraintsblurView.constraints对maskView应用与在blurview上已存在的相同的约束吗?我认为这不会像预期的那样起作用,因为约束绑定到它们创建时使用的视图/锚点,而不是应用于任何视图的通用锚点。尝试显式设置约束。在故事板的blurView上设置了哪些约束?谢谢!!你是对的。根据你的回答,我找到了一个更干净的解决办法。重写func viewdilayoutsubviews{super.viewdilayoutsubviews self.setupBlur}
override func viewDidAppear() {
    self.setupBlur()
}