简体   繁体   English

如何在Swift 4中从CollectionView的底部到Input Accessory View的顶部设置约束

[英]How to set constraints from the bottom of a collectionView to the top of Input Accessory View in Swift 4

so I am having a bit of a trouble getting one of my constraints right.Basically I am using storyboard to put two views on the screen. 因此我遇到了一个麻烦,要正确设置我的约束之一。基本上,我正在使用情节提要在屏幕上放置两个视图。 One is a UITextView and the other is a collectionView. 一个是UITextView,另一个是collectionView。 CollectionView is underneath UItextView. CollectionView在UItextView下面。 I want my collectionView's bottom constraint to be connected to the top of my Input Accessory View which pops up everytime keyboard appears. 我希望collectionView的底部约束连接到每次键盘出现时都会弹出的Input Accessory View的顶部。 By doing this I am essentially making the textview height dynamic. 通过这样做,我实质上是使textview的高度动态化。 Is there any way I can do this? 有什么办法可以做到吗? Here is a picture The two circles are part of Input Accessory View. 这是一张图片这两个圆圈是输入附件视图的一部分。 I want the collectionview which is the priority levels to be connected to the top of the input accessory view. 我希望将集合视图(即优先级)连接到输入附件视图的顶部。 Here is another picture with detailed labeling and markup: Detailed markup Please help! 这是另一张带有详细标签和标记的图片: 详细标记请帮助!

You can add the constraint programatically with something like this: 您可以使用以下方式以编程方式添加约束:

Option 1: 选项1:

let bottomContraint = NSLayoutConstraint(item: self.collectionView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.textField, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 8.0)

self.view.addConstraint(bottomContraint)

Option 2: 选项2:

Using the storyboard, hold down the control key and drag from the textfield to the collection view, then click on vertical spacing. 使用情节提要,按住Control键并将其从文本字段拖到集合视图,然后单击垂直间距。 You can then tap edit the constraint and check the first item is textField.Top and second item is collectionView.Bottom . 然后,您可以点击编辑约束,并检查第一项是textField.Top和第二项是collectionView.Bottom You can then change the constant, including using a negative number is need be. 然后可以更改常量,包括使用负数是必需的。

First, create a constraint from your accessory to the bottom of the screen. 首先,从附件到屏幕底部创建一个约束。

在此处输入图片说明

And then link it to an IBOutlet in your view controller: 然后将其链接到视图控制器中的IBOutlet:

在此处输入图片说明

You have to react to events of keyboard shown and hide. 您必须对显示和隐藏的键盘事件做出反应。 So you register for notifications from the UIKit about that. 因此,您需要从UIKit注册有关此的通知。 Upon this events you dynamically change your height. 发生此事件时,您可以动态更改身高。

Add this to your implementation of the view controller: 将此添加到视图控制器的实现中:

@IBOutlet weak var constraintToChange: NSLayoutConstraint!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}
//other methods skipped

@objc func keyboardWillShow(_ notification: Notification) {
    guard let userInfo = notification.userInfo else {
        return //no info to get keyboard size from
    }
    guard let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else {
        return //no keyboard size to determine height
    }

    constraintToChange.constant = keyboardSize.height //+ your desired height above keyboard
}

@objc func keyboardWillHide(_ notification: Notification){
    constraintToChange.constant = 0.0 //+ your desired height above bottom of the screen
}

Now the most important bit . 现在最重要的一点 Your text view has to satisfy to conditions: 您的文本视图必须满足以下条件:

  1. It's height constraint has to be set to "less than or equal" to allow it to shrink. 必须将其高度约束设置为“小于或等于”以使其收缩。
  2. It's content hugging priority has to be of highest value among all of your views. 在所有视图中,具有优先权内容都必须具有最高价值。 This enables shrinking. 这使得收缩。
  3. It' content compression resistance priority has to be low. 它的内容抗压缩性优先级必须低。 This guarantees that it won't block shrinking. 这保证了它不会阻止收缩。

here is a proper tab (the one with ruler) visible when you click on the height constraint of your text view: 单击文本视图的高度约束时,这是一个正确的选项卡(带有标尺的选项卡):

在此处输入图片说明

and what you should see when you open text view (again ruler tab) in interface builder: 以及在界面构建器中打开文本视图(再次为“标尺”选项卡)时应该看到的内容:

在此处输入图片说明

The effect is as shown here: 效果如下所示:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM