Anchor Constraints not Honored in Xcode 10 / iOS 12

∥☆過路亽.° 提交于 2020-01-24 12:18:06

问题


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)
}
}

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.

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.

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.

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

Update 1:

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.

Update 2:

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/52397203/anchor-constraints-not-honored-in-xcode-10-ios-12

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!