使用textField的视图不会显示

时间:2017-02-03 21:55:03

标签: ios swift

我在mainView的底部有一个视图,名为" newMessageArea"。该视图包含一个文本字段。问题是我尝试了一种方法,当文本字段出现时,文本字段的视图出现...但由于某种原因,它不起作用... 这是我试过的...... (viewDidLoad中)

bottomCOnstraint = newMessageArea.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    bottomCOnstraint.isActive = true

    let messageAreaConstrains : [NSLayoutConstraint] = [

        newMessageArea.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        newMessageArea.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        newMessageArea.heightAnchor.constraint(equalTo: newMessageField.heightAnchor, constant: 20),

        newMessageField.leadingAnchor.constraint(equalTo: newMessageArea.leadingAnchor, constant: 10),
        newMessageField.centerYAnchor.constraint(equalTo: newMessageArea.centerYAnchor),
        newMessageField.trailingAnchor.constraint(equalTo: sendButton.leadingAnchor, constant: -10),

        sendButton.trailingAnchor.constraint(equalTo: newMessageArea.trailingAnchor, constant: -10),
        sendButton.centerYAnchor.constraint(equalTo: newMessageField.centerYAnchor),

    ]

    NSLayoutConstraint.activate(messageAreaConstrains)

NotificationCenter.default.addObserver(
        self,
        selector: #selector(ChatViewController.keyboardWillShow(notification:)),
        name: NSNotification.Name.UIKeyboardWillShow,
        object: nil
    )

这是方法......

func keyboardWillShow(notification: NSNotification){

    if let userInfo = notification.userInfo,

        let frame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue,

        let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue {

        let newFrame = view.convert(frame, from: (UIApplication.shared.delegate?.window)!)

        bottomCOnstraint.constant = newFrame.origin.y - view.frame.height

        UIView.animate(withDuration: animationDuration, animations: {

            self.view.layoutIfNeeded()

        })
    }
}

那么,我该怎么办? 感谢

0 个答案:

没有答案