title: converting storyboard constraints into code
body: <p>I'm trying to programmatically set up constraints for a class in my app so that when the keyboard appears everything moves up. i already got it working before in the storyboard, but when i take what it says in the size inspector and write it in code format, it doesn't work as expected.</p>
  
  <pre><code>...
  //memo area
  var memoArea = UITextView(frame: CGRectMake(20, 291, 275, 225))
  memoArea.backgroundColor = majorColor
  memoArea.delegate = self
  self.view.addSubview(memoArea)
  
  var memoLine = customShadow(theself: self.view, frame: memoArea.frame)
  
  //Spacer View
  var spacer:UIView = UIView(frame: CGRectMake(84, 518, 160, 6))
  spacer.alpha = 0
  self.view.addSubview(spacer)
  
  //Constraints
  var memoAreaToSpacer:NSLayoutConstraint = NSLayoutConstraint(item: spacer, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: memoArea, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 8)
  spacerToBottom = NSLayoutConstraint(item: bottomLayoutGuide, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: spacer, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
  view.addConstraint(memoAreaToSpacer)
  view.addConstraint(spacerToBottom)
  ...
  </code></pre>
  
  <p>so when a keyboard fires a notification this happens.</p>
  
  <pre><code>func updateBottomLayoutConstraintWithNotification(notification: NSNotification) {
      let userInfo = notification.userInfo!
  
  
  let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as NSNumber).doubleValue
  let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
  let convertedKeyboardEndFrame = view.convertRect(keyboardEndFrame, fromView: view.window)
  let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as NSNumber).unsignedIntValue &lt;&lt; 16
  let animationCurve = UIViewAnimationOptions.init(UInt(rawAnimationCurve))
  
  let frame = self.tabBarController?.tabBar.frame
  let height = frame?.size.height
  spacerToBottom.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame) - height! - 5
  
  UIView.animateWithDuration(animationDuration, delay: 0.0, options: .BeginFromCurrentState | animationCurve, animations: {
      self.view.layoutIfNeeded()
      }, completion: nil)
  </code></pre>
  
  <p>}</p>
  
  <p>Here's the full app <a href="https://github.com/stanchiang/phoneHub" rel="nofollow">https://github.com/stanchiang/phoneHub</a></p>
  
  <p>The code i'm talking about is in the BaseDetailViewController.swift in viewDidLoad().</p>
  
  <p>The constraints i'm transferring from is the Edit Controller in the Main.storyboard file.</p>
  
  <p>also, for reference i got it working through storyboard through this blog post.</p>
  
  <p><a href="http://effortlesscode.com/auto-layout-keyboard-shown-hidden/" rel="nofollow">http://effortlesscode.com/auto-layout-keyboard-shown-hidden/</a></p>
  
  <p>thanks for your answers, still trying to get the hang of autolayout so general tips are welcome too. </p>