UIViews
contain information about their own appearance and content.
NSLayoutConstraints
are often added to UIViews to modify how they're distributed across the screen.
You're receiving an error because you're attempting to access a property, constant
, that is not found in UIViews
. You're close though. You need a NSLayoutConstraint.
Step 1: Set Constraints
You can do this programmatically (read more here) or with the help of storyboards / nibs. Here's a step-by-step for the storyboard/nib approach:
- Add a constraint in your storyboard or nib with auto layout.
- Create an
NSLayoutConstraint
variable in your UIView class.
- Back in your storyboard, find your newly-created constraint. It'll show up in your view navigator on the left, next to your
UIView
.
- Select the constraint your looking for, and navigate to the rightmost tab of Xcode's right-hand panel.
- Drag a "New Referencing Outlet" from the right panel to your
UIViewController
class in your view navigator.
- If done correctly, an option for your new
NSLayoutConstraint
will appear. Select it.
Now the variable in your code is references the constraint you made visually. If these steps are confusing and you're very much a beginner (I can't quite tell) please reference this thorough guide to storyboards.
Step 2: Putting it Together
Access your NSLayoutConstraint
's constant
value. Your code should look like something like this once all of the above steps are complete:
// In your class's definitions.
// This is what you'll have to link programmatically or in your storyboard/nib
@IBOutlet var changePassViewBottomConstraint: NSLayoutConstraint!
// Later on in your function:
self.changePassViewBottomConstraint.constant = -1 * keyboardRect.height/2