简体   繁体   English

如何以编程方式更改IB中设置的自动版式约束?

[英]How to Programmatically Change an Auto-Layout constraint, set in IB?

This question seems to come close to what I need, but not quite... 这个问题似乎接近我所需要的,但还不完全是...

I have a UIView (UITableView, to be precise) that has an assigned set of AL constraints, usually from IB (It's supposed to be a reusable class, so that's why "usually"). 我有一个UIView(准确地说是UITableView),它具有一组分配的AL约束,通常是从IB来的(应该是可重用的类,所以这就是“通常”的原因)。

I need to nudge the bottom when the keyboard pops up. 当键盘弹出时,我需要轻推底部。 I know how to get the keyboard events and the keyboard size, so getting the number and the execution thread isn't a problem. 我知道如何获取键盘事件和键盘大小,因此获取数字和执行线程不是问题。

Nudging the bottom is. 摸底。 Since this is a reusable class, and the controller's purview does not extend outside the UITableView (which is where the table's lower AL constraint lives), I can't do the "tie the constraint to the controller" thing that I'd otherwise do. 由于这是一个可重用的类,并且控制器的权限不会扩展到UITableView之外(这是表的较低AL约束所在的位置),所以我无法做“约束约束与控制器”的事情。

This means that I need to get the constraint programmatically. 这意味着我需要以编程方式获取约束。 If there's no constraint, then I need to know that, so I can tell the view to hitch up its skirts the old-fashioned way. 如果没有限制,那么我需要知道这一点,因此我可以告诉视图以老式的方式束缚它的边缘。

If there is a layout constraint, then I need to get the one that anchors the bottom of the table view. 如果存在布局约束,那么我需要获得锚定表视图底部的约束。

Since there could be all kinds of junk there (like people assigning multiple constraints), I can't just grab the first constraint in the anchor. 由于那里可能有各种各样的垃圾(例如分配多个约束的人),所以我不能只抓住锚点中的第一个约束。

I have a feeling that there's no actual solution to this, and I'll end up extending the bottom of the table view's scrollable content size, and hitching the contentOffset. 我觉得这没有实际的解决方案,我将最终扩展表格视图可滚动内容大小的底部,并单击contentOffset。 That will work, but it won't be as "cool." 那会起作用,但不会那么“酷”。 :) :)

Any ideas? 有任何想法吗?

You can link the constraint from IB to your code file just like you link your views. 您可以将约束从IB链接到代码文件,就像链接视图一样。

  • open the document outline 打开文件大纲
  • Look for your tableView 寻找你的tableView
  • expand its constraints 扩大约束
  • right-click drag from the constraint to your code file to create an outlet to the constraint 右键单击从约束拖动到代码文件以创建约束的出口

after that you can do this in code 之后,您可以在代码中执行此操作

constraintOutletName.constant = 100.0 //set the value of the constraint
override func viewDidLoad() {
    super.viewDidLoad()            
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)    
}

@objc func keyboardWillShow(notification: NSNotification) {        
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

        self.tableViewBotCon.constant = -1 * keyboardSize.height

        self.view.layoutIfNeeded()

    }        
}

@objc func keyboardWillHide(notification: NSNotification) {

      self.tableViewBotCon.constant = 0

      self.view.layoutIfNeeded()
}

And the answer is... can I have the envelope, please? 答案是... 我可以拿信封吗? ... ...

"Yew Cain't Git Thar F'm Here." “红豆根本不会在这里。” -At least, not in a sane manner. -至少不是理智的。

Mainly because of the chaotic number of constraints that can be attached to an anchor point (including none that fit the bill). 主要是因为可以附加到锚点的约束数量混乱(包括不符合要求的约束)。

I'm solving the issue much more simply, using the technique I already was working on (and alluded by the answers above). 我使用已经在研究的技术(并通过上面的答案暗示)更加简单地解决了这个问题。

I'll greencheck the answer, even though it basically said to do what I'm already doing, since it was the correct way to address the issue. 我将对答案进行绿色检查,即使它基本上说可以完成我已经在做的事情,因为这是解决问题的正确方法。

[UPDATE] Well, this is annoying. [更新] 好吧,这很烦人。 Apparently, on the iPad, it already does this in the OS, so I'm going to need to add a dratted “idiom” check. 显然,在iPad上,它已经在操作系统中执行了此操作,因此我将需要添加一个草绘的“成语”检查。

[UPDATE 2] It's not that simple. [更新2] 并不是那么简单。 It appears as if the iPad describes a keyboard twice the height of the one on the screen. 似乎iPad所描述的键盘高度是屏幕上键盘高度的两倍。

[UPDATE 3] Well... lookee here what I found . [更新3] 好吧... 在这里,我发现了什么

[UPDATE 4] Turns out the issue is about the opening animation. [UPDATE 4] 事实证明,该问题与开始动画有关。 I needed to look a bit further at the keyboard record. 我需要进一步研究一下键盘记录。 I was looking at the Y origin, when I should have been looking at the height. 当我应该查看高度时,我正在查看Y原点。

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

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