CollectionView单元格隐藏在元素后面

时间:2018-05-08 09:55:20

标签: ios swift xcode scroll uicollectionview

我正在制作这个聊天应用程序,我有一个问题,希望有人可以帮助我。在您可以看到的图像中,我有一个聊天中所有消息的集合视图,这里的问题是还有一条消息在图像中看不到,因为它隐藏在输入文本和按钮后面。如果我按住鼠标并向下滚动,我可以看到它,但是当我放开它时,它会弹回到我看不到最后一条消息的地方。我不知道问题是什么,所以我真的不知道要与你分享什么代码,但也许你知道问题可能是什么以及代码的哪个部分必须修复?

图片:

enter image description here

编辑约束代码:

    view.addSubview(messageInputContainerView)
    view.addConstraintsWithFormat(format: "H:|[v0]|", views: messageInputContainerView)
    view.addConstraintsWithFormat(format: "V:[v0(48)]", views: messageInputContainerView)

    bottomConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
    view.addConstraint(bottomConstraint!)

    setupInputComponents()

此外,这是在viewDidLoad:

String

2 个答案:

答案 0 :(得分:1)

这是将textview和按钮添加为viewcontroller附件视图的更好解决方案

如果您已经看过ios的原生消息应用程序,则可以通过交互式轻扫手势来解除键盘。 这样你的问题也将得到解决,因为我们没有将文本视图和按钮添加到uiview作为子视图,它将作为视图控制器的附件视图

第1步:(可选)

转到Storybaord选择集合视图并在属性列表中将键盘关闭模式更改为交互式解除

enter image description here

并使您的收藏视图填满整个屏幕(不要为textField和发送按钮添加空间)

第2步:

在视图控制器中,在顶部添加以下属性

var viewAcc: UIView?
var sendButton: UIButton!
var inputTextField: UITextField!

override var inputAccessoryView: UIView? {
    return viewAcc
}

override var canBecomeFirstResponder: Bool {
    return true
}

在ViewDidLoad方法中,为init视图添加以下代码,并添加textField和发送按钮

    viewAcc = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
    viewAcc?.backgroundColor = UIColor.white
    inputTextField = UITextField (frame: CGRect(x:8, y:0, width:UIScreen.main.bounds.width, height: 44 ))
    inputTextField.inputAccessoryView = nil
    inputTextField.delegate = self as? UITextFieldDelegate
    inputTextField.placeholder = "Enter message..."
    viewAcc?.backgroundColor = .white
    viewAcc?.addSubview(inputTextField);

    let topBorderView = UIView()
    topBorderView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
    viewAcc?.addSubview(topBorderView)
    viewAcc?.addConstraintsWithFormat(format: "H:|[v0]|", views: topBorderView)
    viewAcc?.addConstraintsWithFormat(format: "V:|[v0(0.5)]", views: topBorderView)

    sendButton = UIButton(type: .system)
    sendButton.isEnabled = true
    sendButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
    sendButton.setTitle("Send", for: .normal)
    sendButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
    sendButton.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
    viewAcc?.addSubview(sendButton)

    inputTextField.translatesAutoresizingMaskIntoConstraints = false
    sendButton.translatesAutoresizingMaskIntoConstraints = false
    viewAcc?.addConstraint(NSLayoutConstraint(item: inputTextField, attribute: .left, relatedBy: .equal, toItem: viewAcc, attribute: .left, multiplier: 1, constant: 8))
    viewAcc?.addConstraint(NSLayoutConstraint(item: inputTextField, attribute: .top, relatedBy: .equal, toItem: viewAcc, attribute: .top, multiplier: 1, constant: 7.5))
    viewAcc?.addConstraint(NSLayoutConstraint(item: inputTextField, attribute: .right, relatedBy: .equal, toItem: sendButton, attribute: .left, multiplier: 1, constant: -2))
    viewAcc?.addConstraint(NSLayoutConstraint(item: inputTextField, attribute: .bottom, relatedBy: .equal, toItem: viewAcc, attribute: .bottom, multiplier: 1, constant: -8))
    viewAcc?.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .right, relatedBy: .equal, toItem: viewAcc, attribute: .right, multiplier: 1, constant: 0))
    viewAcc?.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .bottom, relatedBy: .equal, toItem: viewAcc, attribute: .bottom, multiplier: 1, constant: -4.5))

现在运行应用程序,您可以在底部看到textField和按钮,还可以看到完整的集合视图。只需按住并向下滑动即可关闭键盘

希望它有用

答案 1 :(得分:0)

ios swift 4.2上改进的代码显示了文本按钮。就像在上面的代码中键入sendButton一样,它会自动隐藏,因此我对其进行了修改以保持一致

collectionView.keyboardDismissMode = .interactive
    viewAcc = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
    viewAcc?.backgroundColor = UIColor.white
    inputTextField = UITextField (frame: CGRect(x:8, y:0, width:400, height: 44 ))
    inputTextField.inputAccessoryView = nil
    inputTextField.delegate = self as? UITextFieldDelegate
    inputTextField.placeholder = "Enter message..."
    viewAcc?.backgroundColor = .white
    viewAcc?.addSubview(inputTextField);

    let topBorderView = UIView()
    topBorderView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
    viewAcc?.addSubview(topBorderView)
    viewAcc?.addConstraintsInViews(format: "H:|[v0]|", views: topBorderView)
    viewAcc?.addConstraintsInViews(format: "V:|[v0(0.5)]", views: topBorderView)

    sendButton = UIButton(type: .system)
    sendButton.isEnabled = true
    sendButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
    sendButton.setTitle("Send", for: .normal)
    sendButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
    sendButton.addTarget(self, action: #selector(addmessage), for: .touchUpInside)
    viewAcc?.addSubview(sendButton)

    inputTextField.translatesAutoresizingMaskIntoConstraints = false
    sendButton.translatesAutoresizingMaskIntoConstraints = false
    viewAcc?.addConstraintsInViews(format: "H:|-4-[v0][v1(60)]|", views: inputTextField,sendButton)
    viewAcc?.addConstraintsInViews(format: "V:|-4-[v0]-4-|", views: inputTextField)
    viewAcc?.addConstraintsInViews(format: "V:|-4-[v0]-4-|", views: sendButton)