Как установить ограничение вида входного аксессуара над переключателем приложений / панелью ближе? - PullRequest
0 голосов
/ 02 ноября 2019

У меня возникают трудности с ограничением моего inputAccessoryView над нижней безопасной областью для экранов iPhoneX (и других надрезающих устройств). У меня есть UICollectionViewController, чье inputAccessoryView - это UIView, которое содержит UITextView и 2 UIButtons. В основном макет для приложения чата. Когда я запускаю свой текущий код, inputAccessoryView не появляется над безопасной областью в нижней части экрана. В настоящее время он привязан к нижней части экрана. Прикрепленный скриншот. enter image description here

Как видите, нижний переключатель приложений / панель ближе перекрывает мой inputAccessoryView. Я хотел бы, чтобы inputAccessoryView находился выше указанной панели.

Вот мой текущий код (связанный с ограничениями):

class NewMessagesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    private lazy var inputTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Enter message"
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.delegate = self
        return textField
    }()
    private lazy var inputContainerView: UIView = {
        let margin = view.layoutMarginsGuide
        let safeArea = view.safeAreaInsets

        let containerView = UIView()
        containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
        containerView.backgroundColor = .white

        let uploadImageView = UIImageView()
        uploadImageView.isUserInteractionEnabled = true
        uploadImageView.image = UIImage(named: "image")
        uploadImageView.translatesAutoresizingMaskIntoConstraints = false
        uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleUploadTap)))
        containerView.addSubview(uploadImageView)
        uploadImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        uploadImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        uploadImageView.widthAnchor.constraint(equalToConstant: 44).isActive = true
        uploadImageView.heightAnchor.constraint(equalToConstant: 44).isActive = true

        let sendButton = UIButton(type: .system)
        sendButton.setTitle("Send", for: .normal)
        sendButton.translatesAutoresizingMaskIntoConstraints = false
        sendButton.addTarget(self, action: #selector(sendText), for: .touchUpInside)
        containerView.addSubview(sendButton)
        // x,y,w,h
        sendButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        sendButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
        sendButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

        containerView.addSubview(self.inputTextField)
        self.inputTextField.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor, constant: 8).isActive = true
        self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        self.inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
        self.inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

        let separatorLineView = UIView()
        separatorLineView.backgroundColor = UIColor(red: 220, green: 220, blue: 220)
        separatorLineView.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(separatorLineView)
        separatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        separatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        separatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
        separatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true

        return containerView
    }()

    override var inputAccessoryView: UIView? {
        get {
            return inputContainerView
        }
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title =  ticket.subject

        collectionView.keyboardDismissMode = .interactive
    }
}

Я пытался ограничить нижнюю часть UITextField and UIButton'sограничение якоря равняется view.layoutMarginsGuide.bottomAnchor в viewDidLoad(), но это только заставило мое приложение аварийно завершить работу с ошибкой:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x28204dac0 "UIImageView:0x107417780.bottom"> and <NSLayoutYAxisAnchor:0x2820270c0 "UILayoutGuide:0x280c8cb60'UIViewLayoutMarginsGuide'.bottom"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

Оцените, если кто-нибудь может предоставить фрагмент кода, как правильно ограничить inputAccessoryView над переключателем приложения /ближе бар.

1 Ответ

1 голос
/ 02 ноября 2019

Установите CollectionView Нижние ограничения с SafeArea, Это решит ваши проблемы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...