1
@objc  func handleKeyboardDidShow (notification: NSNotification)
{
    let keyboardRectAsObject = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue

    var keyboardRect = CGRect.zero

    keyboardRectAsObject.getValue(&keyboardRect)

    self.changePassView.constant = -1 * keyboardRect.height/2

    UIView.animate(withDuration: 0.5,animations: {

        self.view.layoutIfNeeded()

    })
}

Can anyone help me? Why I am getting this error as I am a beginner to iOS and learning this language.

  • You should modify a `NSLayoutConstraint` object, which does have a property named `constant` – pckill Feb 11 '20 at 16:18

2 Answers2

0

self.changePassView.constant = -1 * keyboardRect.height/2

You're getting the error because you're trying to access a property called constant in self.changePassView, which is apparently a UIView. It looks like you're trying to change the value of the view's bottom constraint, but a layout constraint is a separate object that you'll have to get before you can set it's value. This might help: How to get constraint "bottomSpace" from UIView Programmatically?

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

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:

  1. Add a constraint in your storyboard or nib with auto layout.
  2. Create an NSLayoutConstraint variable in your UIView class.
  3. Back in your storyboard, find your newly-created constraint. It'll show up in your view navigator on the left, next to your UIView.
  4. Select the constraint your looking for, and navigate to the rightmost tab of Xcode's right-hand panel.
  5. Drag a "New Referencing Outlet" from the right panel to your UIViewController class in your view navigator.
  6. 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
Trent
  • 3
  • 1
  • 6