键盘上的自动布局限制动画显示和隐藏

时间:2018-03-27 19:56:35

标签: ios swift animation constraints nslayoutconstraint

为了使这个简短而甜蜜,我试图复制Facebook登录屏幕的布局和动画。这是:

Facebook login screen

我正试着用徽标开始简单。以下是我设置logoContainerView的方法:

// Logo Container View
logoContainerView = UIImageView(image: #imageLiteral(resourceName: "icons8-golf-ball-64"))
logoContainerView.contentMode = .scaleAspectFit
logoContainerView.translatesAutoresizingMaskIntoConstraints = false
headerContainerView.addSubview(logoContainerView)
// Logo Container AutoLayout
logoContainerView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
logoContainerView.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
logoContainerView.centerXAnchor.constraint(equalTo: headerContainerView.centerXAnchor).isActive = true
logoContainerView.centerYAnchor.constraint(equalTo: headerContainerView.centerYAnchor).isActive = true

现在我只是试图让徽标在xAxis上移动并在显示键盘时调整约束大小。我使用以下代码执行此操作:

fileprivate func observeKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)
}

@objc func keyboardWillShow() {
    logoContainerView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
    logoContainerView.widthAnchor.constraint(equalToConstant: 50.0).isActive = true
    self.updateConstraintsIfNeeded()
    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.layoutIfNeeded()
    }, completion: nil)
}

这确实有效。但是,我收到的内容似乎比warning更多error

2018-03-27 11:47:12.392282-0600 My App[4075:156614] [LayoutConstraints] 
Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x604000284380 UIImageView:0x7ffe84d13390.height == 100   (active)>",
    "<NSLayoutConstraint:0x604000291df0 UIImageView:0x7ffe84d13390.height == 50   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x604000284380 UIImageView:0x7ffe84d13390.height == 100   (active)>

我假设我收到此warning因为我正在尝试设置logoContainerView s heightAnchor,因为我之前已设置此heightAnchor并将其设为活动状态活性

我的最终问题是如何妥善解决这个问题?如何根据键盘是否显示以及何时不显示来设置约束?

感谢任何花时间回应和协助的人。

1 个答案:

答案 0 :(得分:0)

问题在于您目前向现有约束添加约束,这会导致冲突

var hCon:NSLayoutConstraint!
var wCon:NSLayoutConstraint!

//

self.hCon = logoContainerView.heightAnchor.constraint(equalToConstant: 100.0) 
self.hCon.active = true
self.wCon = logoContainerView.widthAnchor.constraint(equalToConstant: 100.0) 
self.wCon.active = true

//

@objc func keyboardWillShow() {
   self.hCon.constant = 50 
   self.wCon.constant = 50
   UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.layoutIfNeeded()
   }, completion: nil)
}