嵌入navbar / tabbar会破坏正常功能视图的约束

时间:2014-12-01 00:38:17

标签: swift ios8 autolayout constraints xcode6.1

我有一个简单的

通常有效的UIViewController:

  1. view.backgroundColor
  2. 的UITextView
  3. UIView(作为底部和textview之间的间隔符)
  4. 将textview固定到视图的约束,而视图又固定在bottomlayoutguide上
  5. 点击textview加载键盘,间隔视图会相应扩展以避免键盘重叠我的textview

  6. ...
    //var memoArea = UITextView(frame: CGRectMake(20, 291, 275, 225))
    memoArea.addConstraint(NSLayoutConstraint(item: memoArea, attribute: .Width, relatedBy: .Equal,
       toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 275.0))
    
    memoArea.addConstraint(NSLayoutConstraint(item: memoArea, attribute: .Height, relatedBy: .Equal,
        toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 225.0))
    
    self.view.addConstraint(NSLayoutConstraint(item: memoArea, attribute: .Leading, relatedBy: .Equal,
        toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 20.0))
    
    // var spacer:UIView = UIView(frame: CGRectMake(84, 518, 160, 6))
    spacer.addConstraint(NSLayoutConstraint(item: spacer, attribute: .Width, relatedBy: .Equal,
       toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 160.0))
    
    spacer.addConstraint(NSLayoutConstraint(item: spacer, attribute: .Height, relatedBy: .Equal,
        toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 6.0))
    
    self.view.addConstraint(NSLayoutConstraint(item: spacer, attribute: .Leading, relatedBy: .Equal,
        toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 84.0))
    
    view.setTranslatesAutoresizingMaskIntoConstraints(false)
    spacer.setTranslatesAutoresizingMaskIntoConstraints(false)
    memoArea.setTranslatesAutoresizingMaskIntoConstraints(false)
    ...
    

    ...
    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 << 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)
    ...
    

    但是,标签栏导航栏添加到具有正常工作约束的视图后,

    3件事情破裂:

    1. 背景不再呈现,产生黑色背景
    2. textview没有注册点击,即键盘没有加载
    3. 视图忽略了bottomlayoutguide。它只是将我的对象移动到它们的顶部.Top约束允许它们。 textview和uiview之间的约束仍然很受尊重。

1 个答案:

答案 0 :(得分:0)

注释掉view.setTranslatesAutoresizingMaskIntoConstraints(false)摆脱了所有错误。