我有一个根本没有显示的容器视图。标签栏随背景一起消失。容器视图将包含一个textField,屏幕右边有一个发送按钮。我错过了什么或不应该做什么?
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])
}
}