当我以编程方式更改约束时,Xcode给出错误

问题描述

我有一个用viewDidLoad()方法中的4个约束初始化的表视图。我想稍后在代码中以编程方式更改bottomAnchor,因此将其保存为变量bottomAnchor,然后为更改后的约束添加了另一个变量keyboardBottomAnchor

这些是初始约束:

tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor,constant: 50).isActive = true
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
bottomAnchor = tableViewe.bottomAnchor.constraint(equalTo: self.view.bottomAnchor,constant:  (-1) * card.cardHandleAreaHeight + -20)
bottomAnchor.isActive = true

基本上,我希望表视图在键盘打开时上升,而在键盘关闭时下降。外观如下:

@objc func keyboardAppears(notification: Notification) {
        let userInfo = notification.userInfo
        keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
       
        //this is the original constraint
        self.bottomConstraint.isActive = false
        //Here I make a new variable to save the new constraint
        keyboardBottomConstraint = tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor,constant: -1 * keyboardFrame.height)
        keyboardBottomConstraint.isActive = true
}

@objc func keyboardDisappears(notification: Notification) {
        NSLayoutConstraint.deactivate([keyboardBottomConstraint])
        bottomConstraint.isActive = true
}

keyboardAppears方法正在起作用(当键盘显示时,表格视图会上升),但是keyboardDisappears方法却给了我一个Unable to simultaneously satisfy constraints错误(也就是说两个{{ 1}}和bottomConstraint有效)

关于为什么会发生这种情况的任何想法?

更新:

我使用了下面的keyboardBottomConstraint(此方法有效,但仅在我第一次打开键盘时可用)

.constant

解决方法

雨燕5

请勿停用约束,只需更改其常数值即可。

我已经在下面编写了代码并对其进行了测试,可以根据需要对其进行自定义,将与您完美配合!

import UIKit

class ViewController: UIViewController
{
    // MARK: Properties

    var tableView: UITableView!
    var tableBottomConstraint: NSLayoutConstraint!

    // MARK: View Controller Life Cycle

    override func viewDidLoad()
    {
        super.viewDidLoad()
        addKeyboardObservers()
        setupTableView()
    }

    deinit
    {
        removeKeyboardObservers()
    }

    // MARK: Methods

    private func addKeyboardObservers()
    {
        NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillShow(notification:)),name: UIResponder.keyboardWillShowNotification,object: nil)

        NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillHide(notification:)),name: UIResponder.keyboardWillHideNotification,object: nil)
    }

    private func removeKeyboardObservers()
    {
        NotificationCenter.default.removeObserver(self,object: nil)

        NotificationCenter.default.removeObserver(self,object: nil)
    }

    private func setupTableView()
    {
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(tableView)

        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 0.0).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: 0.0).isActive = true
        tableView.topAnchor.constraint(equalTo: view.topAnchor,constant: 50.0).isActive = true

        tableBottomConstraint = tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -50.0)
        tableBottomConstraint.isActive = true
    }

    // MARK: Keyboard Handling

    @objc func keyboardWillShow(notification: Notification)
    {
        if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
        {
            tableBottomConstraint.constant = -1 * keyboardFrame.height
            loadViewIfNeeded()
        }
    }

    @objc func keyboardWillHide(notification: Notification)
    {
        tableBottomConstraint.constant = -50.0
        loadViewIfNeeded()
    }
}

注意:不要忘记删除deinit中的键盘观察者

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...