How to integrate safeAreaInsets to current constraints extension - ios

I created extension for the view's constraints. You can find the code in the below but after new release iPhone X we need to use safeAreaInsets but i don't know how implement that property. I'll be so glad if you could help me out.
Thanks
extension UIView {
func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
}
if height != 0 {
heightAnchor.constraint(equalToConstant: height).isActive = true
}
if width != 0 {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
}
}

I have refactored your code to take into account the insets. Try the following:
import UIKit
extension UIView {
func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, enableInsets: Bool) {
var topInset = CGFloat(0)
var bottomInset = CGFloat(0)
var rightInset = CGFloat(0)
var leftInset = CGFloat(0)
if #available(iOS 11, *), enableInsets {
let insets = self.safeAreaInsets
topInset = insets.top
bottomInset = insets.bottom
rightInset = insets.right
leftInset = insets.left
}
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: paddingTop+topInset).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left, constant: paddingLeft+leftInset).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -paddingRight-rightInset).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset).isActive = true
}
if height != 0 {
heightAnchor.constraint(equalToConstant: height).isActive = true
}
if width != 0 {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
}
}

Related

Add Top border to selected TabBar

How to add border to the TOP of selected TabBar.
Below method I have used to add it at bottom but I want
extension UIImage {
func createSelectionIndicator(color: UIColor, size: CGSize, lineWidth: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRect(x: 0, y: size.height - lineWidth, width: size.width, height: lineWidth))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: BLUE, size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count), height: tabBar.frame.height), lineWidth: 2.0)
You can use the function to create borders on any side -
enum Side: String {
case top, left, right, bottom
}
extension UIImage {
func createSelectionIndicator(color: UIColor, size: CGSize, lineThickness: CGFloat, side: Side) -> UIImage {
var xPosition = 0.0
var yPosition = 0.0
var imgWidth = 2.0
var imgHeight = 2.0
switch side {
case .top:
xPosition = 0.0
yPosition = 0.0
imgWidth = size.width
imgHeight = lineThickness
case .bottom:
xPosition = 0.0
yPosition = size.height - lineThickness
imgWidth = size.width
imgHeight = lineThickness
case .left:
xPosition = 0.0
yPosition = 0.0
imgWidth = lineThickness
imgHeight = size.height
case .right:
xPosition = size.width - lineThickness
yPosition = 0.0
imgWidth = lineThickness
imgHeight = size.height
}
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRect(x: xPosition, y: yPosition, width: imgWidth, height: imgHeight))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: BLUE, size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count), height: tabBar.frame.height), lineThickness: 2.0, side: .top)
Let me know if this was useful or you need any other help.
Happy Coding :)
import UIKit
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.tabBar.addBorder(.top, color: .red, thickness: 2.0)
}
}
extension UITabBar {
func addBorder(_ edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let subview = UIView()
subview.translatesAutoresizingMaskIntoConstraints = false
subview.backgroundColor = color
self.addSubview(subview)
switch edge {
case .top, .bottom:
subview.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
subview.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
subview.heightAnchor.constraint(equalToConstant: thickness).isActive = true
if edge == .top {
subview.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
} else {
subview.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
case .left, .right:
subview.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
subview.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
subview.widthAnchor.constraint(equalToConstant: thickness).isActive = true
if edge == .left {
subview.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
} else {
subview.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
}
default:
break
}
}
}
tabBarController?.tabBar.addBorder(.top, color: .orange, thickness: 5.0)

How to make custom class for auto layouts?

Like we make custom classes for UIButton and other items of UIKit.
Just we define our layouts in that class and further use them as we do in case of other custom class.
If it can not happen, kindly comment that why it can't be done?
The below extension can be used for your requirement
extension UIView {
public func anchor(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
var anchors = [NSLayoutConstraint]()
if let top = top {
anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant))
}
if let left = left {
anchors.append(leftAnchor.constraint(equalTo: left, constant: leftConstant))
}
if let bottom = bottom {
anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant))
}
if let right = right {
anchors.append(rightAnchor.constraint(equalTo: right, constant: -rightConstant))
}
if widthConstant > 0 {
anchors.append(widthAnchor.constraint(equalToConstant: widthConstant))
}
if heightConstant > 0 {
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant))
}
anchors.forEach({$0.isActive = true})
}
public func anchor(centerX: NSLayoutXAxisAnchor? = nil, centerY: NSLayoutYAxisAnchor? = nil, xConstant: CGFloat = 0, yConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) {
self.translatesAutoresizingMaskIntoConstraints = false
var anchors = [NSLayoutConstraint]()
if let centerX = centerX {
anchors.append(centerXAnchor.constraint(equalTo: centerX, constant: xConstant))
}
if let centerY = centerY {
anchors.append(centerYAnchor.constraint(equalTo: centerY, constant: yConstant))
}
if widthConstant > 0 {
anchors.append(widthAnchor.constraint(equalToConstant: widthConstant))
}
if heightConstant > 0 {
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant))
}
anchors.forEach({$0.isActive = true})
}
}
as
label.anchor(centerX: view.centerXAnchor, centerY: view.centerYAnchor, xConstant: 20, yConstant: 5, widthConstant: screenSize.width, heightConstant: height1)
and
label.anchor(top: nil, left: customCell.leftAnchor, bottom: customCell.bottomAnchor, right: customCell.rightAnchor, topConstant: 0, leftConstant: 22, bottomConstant: 0, rightConstant: 22, widthConstant: width, heightConstant: btnHeigght)

How do I have to modify my UIView extension if I wanted to animate the auto layout constraints afterward?

I have made an UIView extension to set my NSLayout anchors. Everything works just fine. But how can I modify my extension if I wanted to add an NSLayoutConstraint so I could animate the constraints afterward?
Here is my current extension:
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) {
//translate the view's autoresizing mask into Auto Layout constraints
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true
}
if let leading = leading {
leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom).isActive = true
}
if let trailing = trailing {
trailingAnchor.constraint(equalTo: trailing, constant: -padding.right).isActive = true
}
if size.width != 0 {
widthAnchor.constraint(equalToConstant: size.width).isActive = true
}
if size.height != 0 {
heightAnchor.constraint(equalToConstant: size.height).isActive = true
}
}
}
And here is how the extension is called:
//feedViewButton constraints
feedViewButton.anchor(top: nil, leading: nil, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 25), size: .init(width: 50, height: 50))
UPDATE
I would like to have something like this
var topAnchor: NSLayoutConstraint?
topAnchor = topAnchor.constraint(equaltTo: top, constant: padding.top)
topAnchor.isActive = true
And then animate it like this:
let animator = UIViewPropertyAnimator(duration: 1, curve: .easeOut) {
topAnchor.constant = 20
}
animator.startAnimation()
Just try to animate it using autolayout, since the constraints haven't been applied yet, it should be animatable:
feedViewButton.anchor(top: nil, leading: nil, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 25), size: .init(width: 50, height: 50))
feedViewButton.superview?.setNeedsLayout()
UIView.animate(withDuration: 0.2, delay: 0, options: [.allowUserInteraction], animations: {
feedViewButton.superview?.layoutIfNeeded()
}, completion: nil)
UPDATE
If you want to later change the constants on the anchors to animate some other change, you have to keep references to the constraints to be able to manipulate them later:
enum ConstraintType {
case top, leading, trailing, bottom, width, height
}
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> [ConstraintType : NSLayoutConstraint] {
//translate the view's autoresizing mask into Auto Layout constraints
translatesAutoresizingMaskIntoConstraints = false
var constraints: [ConstraintType : NSLayoutConstraint] = [:]
if let top = top {
constraints[.top] = topAnchor.constraint(equalTo: top, constant: padding.top)
}
if let leading = leading {
constraints[.leading] = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
}
if let bottom = bottom {
constraints[.bottom] = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
}
if let trailing = trailing {
constraints[.trailing] = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
}
if size.width != 0 {
constraints[.width] = widthAnchor.constraint(equalToConstant: size.width)
}
if size.height != 0 {
constraints[.height] = heightAnchor.constraint(equalToConstant: size.height)
}
let constraintsArray = Array<NSLayoutConstraint>(constraints.values)
NSLayoutConstraint.activate(constraintsArray)
return constraints
}
}
This extension returns a dictionary of constraints that you can later change and animate those changes. E.g.:
let constraints = feedViewButton.anchor(top: nil, leading: nil, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 25), size: .init(width: 50, height: 50))
// applies the constraints
self.view.layoutIfNeeded()
// now to animate bottom to -50:
if let bottomConstraint = constraints[.bottom] {
bottomConstraint.constant = -50
self.view.setNeedsLayout()
let animator = UIViewPropertyAnimator(duration: 1, curve: .easeInOut, animations: {
self.view.layoutIfNeeded()
})
animator.startAnimation()
}
You can simply add the animation on the extension, so all calls to anchor will be animated without add the animation block always.
extension UIView {
func anchor(animated: Bool, top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) {
//translate the view's autoresizing mask into Auto Layout constraints
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true
}
if let leading = leading {
leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom).isActive = true
}
if let trailing = trailing {
trailingAnchor.constraint(equalTo: trailing, constant: -padding.right).isActive = true
}
if size.width != 0 {
widthAnchor.constraint(equalToConstant: size.width).isActive = true
}
if size.height != 0 {
heightAnchor.constraint(equalToConstant: size.height).isActive = true
}
if animated {
UIView.animate(withDuration: 0.2, delay: 0, options: [.allowUserInteraction], animations: {
self.layoutIfNeeded()
}, completion: nil)
} else {
self.layoutIfNeeded()
}
}
}

Cannot set button image inside UIStackView

I have a UICollectionViewCell which has a UIStackView that fills the whole cell.
Inside that UIStackView I am trying to add subviews but I'm running into two problems here.
First one is that I cannot see the UIStackView being added to the cell, even setting the stack view background color to a different one it doesn't show.
The second problem is that my app - in this case it is a Today Extension Widget crashes when I'm instantiating a button image as button.setImage(:_)
Here's the code:
#objc lazy var composeButton: UIButton = {
let button = UIButton(type: .system)
let image = #imageLiteral(resourceName: "compose_icon").withRenderingMode(.alwaysOriginal)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(didTapCompose), for: .touchUpInside)
return button
}()
let stackview = UIStackView(arrangedSubviews: [composeButton, reloadButton])
stackview.distribution = .fillEqually
stackview.axis = .horizontal
addSubview(stackview)
stackview.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddinfLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
extension UIView {
/// Anchor a specific view to a superview
#objc func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddinfLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: paddinfLeft).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
}
if width != 0 {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
if height != 0 {
heightAnchor.constraint(equalToConstant: height).isActive = true
}
}
/// Rounds the corners of a UIView object
/// parameters: corners - the corners to round (upperLeft, upperRight, lowerLeft, lowerRight);
/// radiud - the desired cornerRadius to apply to the desired corners
#objc func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
The app crashes on this line
button.setImage(image, for: .normal)
It crashes with a SIGABRT error as shown in the image bellow.
Does anyone spot the problem here ?
Since this is part of a Today Extension Widget, make sure you've included the image in that build target.
A future debugging tip: When you see an error that "really shouldn't happen," try thinking about step-by-step. In this case, while the .setImage line appeared to be the culprit, if you had checked the validity of your let image = line, you might have saved a little time.

UIView subviews not appearing

I have a view controller with a UIView. Inside that UIView I add elements like a text label. When I run my code it is not there.
import UIKit
class EventDetailViewController: UIViewController {
//variables that will hold data sent in through previous event controller
var eventImage = ""
var eventName = ""
var eventDescription = ""
var eventStreet = ""
var eventCity = ""
var eventState = ""
var eventZip = 0
var eventDate = ""
//
lazy var currentEventImage : UIImageView = {
let currentEvent = UIImageView()
let imageURL = URL(string: self.eventImage)
currentEvent.af_setImage(withURL: imageURL!)
currentEvent.clipsToBounds = true
currentEvent.translatesAutoresizingMaskIntoConstraints = false
currentEvent.contentMode = .scaleToFill
currentEvent.layer.masksToBounds = true
return currentEvent
}()
//will be responsible for creating the UIView that contains relevant event information
let eventInfoContainerView: UIView = {
let infoContainer = UIView()
infoContainer.backgroundColor = UIColor.white
infoContainer.layer.masksToBounds = true
return infoContainer
}()
lazy var eventNameLabel: UILabel = {
let currentEventName = UILabel()
currentEventName.text = self.eventName
currentEventName.translatesAutoresizingMaskIntoConstraints = false
return currentEventName
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
navigationItem.title = eventName
self.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(image: UIImage(named: "icons8-Back-64"), style: .plain, target: self, action: #selector(GoBack))
self.navigationItem.leftBarButtonItem = backButton
//Subviews will be added here
view.addSubview(currentEventImage)
view.addSubview(eventInfoContainerView)
eventInfoContainerView.addSubview(eventNameLabel)
//Constraints will be added here
_ = currentEventImage.anchor(view.centerYAnchor, left: nil, bottom: nil, right: nil, topConstant: -305, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.view.frame.width, heightConstant: 200)
_ = eventInfoContainerView.anchor(currentEventImage.bottomAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
_ = eventNameLabel.anchor(eventInfoContainerView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 20, leftConstant: 32, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
}
func GoBack(){
_ = self.navigationController?.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am kind of lost I viewed a tutorial where they added elements to a UIView in a similar fashion and things appeared. Any insight would be helpful. Tried changing multiple things and nothing really made it appear and I have researched a couple answers I know it is probably something small that my eyes are just looking past but I can't discover it at this point.
This is my custom method for setting constraints
import UIKit
extension UIView {
func anchorToTop(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil) {
anchorWithConstantsToTop(top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
}
func anchorWithConstantsToTop(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -rightConstant).isActive = true
}
}
func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
var anchors = [NSLayoutConstraint]()
if let top = top {
anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant))
}
if let left = left {
anchors.append(leftAnchor.constraint(equalTo: left, constant: leftConstant))
}
if let bottom = bottom {
anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant))
}
if let right = right {
anchors.append(rightAnchor.constraint(equalTo: right, constant: -rightConstant))
}
if widthConstant > 0 {
anchors.append(widthAnchor.constraint(equalToConstant: widthConstant))
}
if heightConstant > 0 {
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant))
}
anchors.forEach({$0.isActive = true})
return anchors
}
}
OK - now that we see your .anchor(...) function...
The TOP of your label is set to the BOTTOM of eventInfoContainerView with topConstant: 20 ... which is +20
Change that value to -20 (assuming you want the label along the bottom of the view):
_ = eventNameLabel.anchor(eventInfoContainerView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: -20, leftConstant: 32, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
Assuming your .anchor(...) function / extension is working properly, I think the only thing wrong is that you forgot to set:
infoContainer.translatesAutoresizingMaskIntoConstraints = false
when you initialize your eventInfoContainerView

Resources