UICollectionView Self Sizing Cells with Auto Layout

This answer is outdated from iOS 14 with the addition of compositional layouts. Please consider updating the new API Updated for Swift 5 preferredLayoutAttributesFittingAttributes renamed to preferredLayoutAttributesFitting and use auto sizing Updated for Swift 4 systemLayoutSizeFittingSize renamed to systemLayoutSizeFitting Updated for iOS 9 After seeing my GitHub solution break under iOS 9 I finally got … Read more

How to add constraints programmatically using Swift

Do you plan to have a squared UIView of width: 100 and Height: 100 centered inside the UIView of an UIViewController? If so, you may try one of the 6 following Auto Layout styles (Swift 5 / iOS 12.2): 1. Using NSLayoutConstraint initializer override func viewDidLoad() { let newView = UIView() newView.backgroundColor = UIColor.red view.addSubview(newView) … Read more