My code is shown below. It's derived from a YouTube video made 3 years ago. Since the date the video was posted, Swift has updated and the output of the function which is Anchor can no longer be found in scope. I'd like to ask for helping in finding a replacement for the output.
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?,
bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding:
UIEdgeInsets = .zero, size: CGSize = .zero) -> Anchor {
translatesAutoresizingMaskIntoConstraints = false
var anchoredConstraints = AnchoredConstraints()
if let top = top {
anchoredConstraints.top = topAnchor.constraint(equalTo: top,
constant: padding.top)
}
if let leading = leading {
anchoredConstraints.leading = leadingAnchor.constraint(equalTo:
leading, constant: padding.left)
}
if let bottom = bottom {
anchoredConstraints.bottom = bottomAnchor.constraint (equalTo:
bottom, constant: -padding.bottom)
}
if size.width != 0 {
widthAnchor.constraint(equalToConstant:
size.width).isActive = true
}
if size.height != 0 {
heightAnchor.constraint(equalToConstant:
size.height).isActive = true
}
}
I can't suppose your real intention but I believe this snippet(in Swift 5) may be what are you looking for
The following is a playground sample code showing how can you use it. (Please don't forget to include the extension somewhere in the code below if you get it try)
That's the best I can do to help you with no more information, so please in a next post try to give more details about the intentions of the code.
In addition I would advise you that there are better solutions for pinning views in layout nowadays, then I believe it worths a quick research for alternatives.
I Hope it can be of some help to you.