ContainerView не отображается

У меня есть представление контейнера, которое вообще не отображается. Панель вкладок исчезнет вместе с фоном. Представление контейнера будет включать текстовое поле с кнопкой отправки на правом краю экрана. Что я упускаю или не должен делать?

override func viewDidLoad() {

self.tabBarController?.view.removeFromSuperview()
    collectionView?.backgroundColor = .blue
    messageContainerView()
}

func messageContainerView() {

    let containerView = UIView()
    containerView.backgroundColor = .green
    containerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(containerView)

    let messageTextField = UITextField()
    messageTextField.placeholder = "Enter a message"
    messageTextField.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(messageTextField)

    let sendMessageButton = UIButton()
    sendMessageButton.setTitle("Send", for: .normal)
    sendMessageButton.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(sendMessageButton)

    // MARK: create line to separate the message box & messages
    let separatorLineView = UIView()     // https://www.youtube.com/watch?v=FDay6ocBlnE&list=PL0dzCUj1L5JEfHqwjBV0XFb9qx9cGXwkq&index=8&t=609s
    separatorLineView.backgroundColor = .black
    separatorLineView.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(separatorLineView)

    // MARK: Constraints
    let separatorTopLine = separatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor)
    let separatorLeftLine = separatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor)
    let separatorWidthLine = separatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor)
    let separatorHeightLine = separatorLineView.heightAnchor.constraint(equalToConstant: 1)

    // Message container
    let containerViewLeft = containerView.leftAnchor.constraint(equalTo: view.leftAnchor)  // https://stackoverflow.com/questions/37370801/how-to-add-a-container-view-programmatically/40102513
    let containerViewRight = containerView.rightAnchor.constraint(equalTo: view.rightAnchor)
    let containerViewBottom = containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    let containerViewWidth = containerView.widthAnchor.constraint(equalTo: view.widthAnchor)
    let containerViewHeight = containerView.heightAnchor.constraint(equalToConstant: 50)

    // Message textfield
    let messageFieldLeft = messageTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor)
    let messageFieldRight = messageTextField.rightAnchor.constraint(equalTo: containerView.rightAnchor)
    let messageFieldCenter = messageTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor)
    let messageFieldHeight = messageTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor)
    let messageFieldWidth = messageTextField.widthAnchor.constraint(equalToConstant: 100)

    // Send message Button constraints
    let rightMessageButton = sendMessageButton.rightAnchor.constraint(equalTo: containerView.rightAnchor)
    let centerMessageButton = sendMessageButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor)
    let widthMessageButton = sendMessageButton.widthAnchor.constraint(equalToConstant: 80)
    let heightMessageButton = sendMessageButton.heightAnchor.constraint(equalTo: containerView.heightAnchor)

    NSLayoutConstraint.activate([separatorTopLine, separatorLeftLine, separatorWidthLine, separatorHeightLine, containerViewLeft, containerViewRight, containerViewWidth, containerViewHeight, containerViewBottom, rightMessageButton, centerMessageButton, widthMessageButton, heightMessageButton, messageFieldLeft, messageFieldRight,messageFieldCenter, messageFieldHeight, messageFieldWidth])

}

}

person Terril320    schedule 11.06.2017    source источник
comment
Я думаю - я думаю! - вы смешиваете два способа выполнения ваших ограничений. Ваша последняя строка... NSLayoutConstraint.activate([...])... должна содержать массив (что и есть), но не обязательно то, что вы хотите. Попробуйте удалить эту строку, а также добавить .isActive = true ко всем указанным выше ограничениям. (Синтаксис должен быть let myConstraint = myButton.[anchor].constraint(equalTo: [anchor]).isActive = true.)   -  person dfd    schedule 12.06.2017
comment
@dfd.Спасибо, что нашли время ответить. Когда я это сделал, я получил предупреждение об инициализации для ограничений.   -  person Terril320    schedule 12.06.2017