I often see Auto Layout constraints being added in the UIViewController
but to me that seems to be the wrong place for layout logic.
Is it possible to add NSLayoutConstraint
s within a custom UIView
?
Where in a UIView
would be the right place to add them programmatically?
To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.
Select the items you want to align, and then click the Align tool. Interface Builder presents a popover view containing a number of possible alignments. Select the options for aligning the selected views, and click the Add Constraints button. Interface Builder creates the constraints needed to ensure those alignments.
Open the Align menu with the yellow button selected and check Horizontally in Container, then click Add 1 Constraint. Now, select both buttons at the same time using the Shift key and, in the Align menu, check Leading Edges. Again, actually install the constraint by clicking Add 1 Constraint.
Is it possible to add NSLayoutConstraints within a custom UIView?
Yes it is possible to add constraints within a custom view, organization is very important here, especially if you want to animate parts of your custom view.
Read the subclassing section from Apple's UIView Reference document
Constraints:
requiresConstraintBasedLayout - Implement this class method if your view class requires constraints to work properly.
updateConstraints - Implement this method if your view needs to create custom constraints between your subviews.
alignmentRectForFrame:, frameForAlignmentRect: - Implement these methods to override how your views are aligned to other views.
Where in a UIView is the right place to add them programmatically?
Here is a skeleton outline of a custom class. The key concern is that you centralize your constraints otherwise the class become very messy the more constraints you add. Also you can introduce other settings in the updateConstraints() method and conditionally add or remove constraints by setting your configuration values and then call setNeedsUpdateConstraints().
Any constraints you decide you want to animate should most lightly be instance variables.
Hope this helps :)
class MyCustomView: UIView { private var didSetupConstraints = false private let myLabel = UILabel(frame: CGRectZero) // MARK: Lifecycle override init(frame: CGRect) { super.init(frame: CGRectZero) self.setup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.setup() } // Mark: - Setup private func setup() { // 1. Setup the properties of the view it's self self.translatesAutoresizingMaskIntoConstraints = false backgroundColor = UIColor.orangeColor() clipsToBounds = true // 2. Setup your subviews setupMyLabel() // 3. Inform the contraints engine to update the constraints self.setNeedsUpdateConstraints() } private func setupMyLabel() { myLabel.translatesAutoresizingMaskIntoConstraints = false } override func updateConstraints() { if didSetupConstraints == false { addConstraintsForMyLabel() } super.updateConstraints() //Documentation note: Call [super updateConstraints] as the final step in your implementation. } private func addConstraintsForMyLabel() { // Add your constraints here } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With