Xcode 10/iOS 12中未遵守锚定约束 [英] Anchor Constraints not Honored in Xcode 10 / iOS 12

查看:113
本文介绍了Xcode 10/iOS 12中未遵守锚定约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天所有这些都在Xcode 9和iOS 11下运行;但是,在更新到Xcode 10和iOS 12之后,该视图将不再显示.我当时在视图中显示了一个视频.今天,我可以听到但看不到视频.我检查了框架,发现它为零,这说明了问题.但是,与以前的版本相比,没有任何变化.我删除了视频内容,仅在应用锚定约束并且它们全部为零之后,才查看视图的帧.

This all worked yesterday under Xcode 9 and iOS 11; however, after updating to Xcode 10 and iOS 12, the view is no longer showing. I was having a video displayed inside a view. Today I could hear but not see the video. I checked the frame and found it to be zero which explained the issue. However, nothing had changed from the prior version. I have removed the video stuff and have just looked at the view's frame after anchor constraints were applied and they are all zero.

import UIKit
import AVKit

class VideoView: UIView {

private var videoURL:URL!
private var parentView:UIView!
private var avPlayer:AVPlayer!
private var avPlayerLayer:AVPlayerLayer!

init(url:URL, parentView:UIView) {
    super.init(frame: .zero)

    self.videoURL = url
    self.parentView = parentView

    setup()
}

private func setup() {
    self.translatesAutoresizingMaskIntoConstraints = false
    self.parentView.addSubview(self)

    self.topAnchor.constraint(equalTo: self.parentView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
    self.leadingAnchor.constraint(equalTo: self.parentView.leadingAnchor, constant: 8).isActive = true
    self.trailingAnchor.constraint(equalTo: self.parentView.trailingAnchor, constant: -8).isActive = true
    self.heightAnchor.constraint(equalToConstant: 200).isActive = true

    print(self.parentView.frame)
    print(self.frame)
}


override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
}

请注意setup()末尾的两个打印语句. parentView返回0,0,414,736. self视图返回0,0,0,0.

notice the two print statements at the end of setup(). The parentView return 0,0,414,736. The self view returns 0,0,0,0.

如果在Init中设置了帧大小,它将遵循它.但是,它不应用锚点约束,因此视图的大小将保持不变,无论我将其放入什么init中.

If in Init I set the frame size it will respect it; however, it does not apply the anchor constraints so the size of the view will remain whatever it is I put into the init.

似乎根本没有考虑锚约束.调试器中没有关于它们的错误,我们可以看到,translatesAutoresizingMaskIntoConstraints设置为false,并且所有约束都将isActive设置为true.

It appears that the anchor constraints are not at all being taken into account. There is no error in the debugger about them and as we can see, translatesAutoresizingMaskIntoConstraints is set to false and all constraints have the isActive set to true.

仅Xcode和iOS版本已更改.我想念什么?

Only the Xcode and iOS version have changed. What am I missing?

更新1:

如果我创建标签并将其添加到self,则该标签将显示正常.如果我为self创建背景色,则该背景色也可以正常显示,包括根据锚定约束设置的适当高度.但是,帧保持为零.因此,当尝试通过将其框架设置为self的边界来添加AVPlayerLayer时,它不起作用,因为self当然保持为零.因此,问题仍然在于为什么初始化后框架的尺寸没有改变.

If I create a label and add it to self, the label shows fine. If I create a background color for self that also shows fine including the proper height as set with the anchor constraints. However, the frame remains at zero. So when trying to add an AVPlayerLayer by setting its frame to the bounds of self it doesn't work because of course self remains at zero. So the question remains as to why the frame's dimensions are not changing after initialization.

更新2:

我在应用锚点约束后添加了self.layoutIfNeeded(),这似乎已经解决了问题.尽管看了self的框架,但我却得到了-199,-100,398,200不能说我理解X and Y的值.尽管layOutIfNeeded似乎已经解决了问题,但是为什么Xcode 10/iOS 12要求这样做也是一个谜.

I added a self.layoutIfNeeded() just after applying the anchor constraints and that seems to have solved the issue. Although looking at the frame for self and I get -199,-100,398,200 Cannot say I understand the values for X and Y. Nevertheless the layOutIfNeeded seems to have solved the problem although why this is required in Xcode 10/ iOS 12 is also a mystery.

推荐答案

我发布了第二次更新的答案,但为了清楚起见:

I posted what seems to be the answer in my second update, but for clarity:

我在应用锚点约束后添加了self.layoutIfNeeded(),这似乎已经解决了问题.尽管查看自我的框架并得到-199,-100,398,200不能说我理解X and Y的值.尽管如此,尽管为什么在Xcode 10/iOS 12中要求这么做,layoutIfNeeded似乎已经解决了这个问题.

I added a self.layoutIfNeeded() just after applying the anchor constraints and that seems to have solved the issue. Although looking at the frame for self and I get -199,-100,398,200 Cannot say I understand the values for X and Y. Nevertheless the layOutIfNeeded seems to have solved the problem although why this is required in Xcode 10/ iOS 12 is also a mystery.

这篇关于Xcode 10/iOS 12中未遵守锚定约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆