简体   繁体   English

迅捷约束和锚点

[英]Swift Constraints & Anchors

In fact, I was looking for an example of a drop-down button. 实际上,我在寻找一个下拉按钮的示例。 But unfortunately it turned out to be not fully working, and I do not know well how dynamic constraints and anchors work. 但是不幸的是,事实证明它不能完全正常工作,我也不知道动态约束和锚点是如何工作的。 Therefore I ask for your help. 因此,我要求您的帮助。

'NSGenericException', reason: 'Unable to activate constraint with “ NSGenericException”,原因:“无法通过以下方式激活约束
anchors and because they have no common ancestor. 锚,因为他们没有共同的祖先。 Does the constraint or its anchors reference items in different view hierarchies? 约束或其锚点是否引用了不同视图层次结构中的项目? That's illegal. 那是非法的。

You can find my source code on Github: https://github.com/Archetapp/Drop-Down-Menu/blob/master/sdafjkbslib/ViewController.swift 您可以在Github上找到我的源代码: https : //github.com/Archetapp/Drop-Down-Menu/blob/master/sdafjkbslib/ViewController.swift

I found similar problems and their solution was: 我发现了类似的问题,他们的解决方案是:

  • Check whether you added the programmatically created view to its parent before activating constraints. 在激活约束之前,请检查是否已将以编程方式创建的视图添加到其父视图。

  • Check whether you write constraints activation code inside viewDidLoad () / viewWillAppear (). 检查是否在viewDidLoad()/ viewWillAppear()内部编写约束激活代码。 You should write constraints in the updateViewConstraints or viewWillLayoutSubviews. 您应该在updateViewConstraints或viewWillLayoutSubviews中编写约束。

  • Check whether you turn off translatesAutoresizingMaskIntoConstraints. 检查是否关闭了translatesAutoresizingMaskIntoConstraints。

But it did not help me. 但这并没有帮助我。

For dropdown i mostly prefer this library less number of code and easy to handle 对于下拉菜单,我最喜欢此的代码数量少且易于处理

initialize 初始化

let dropDown = DropDown()

// The view to which the drop down will appear on
dropDown.anchorView = view // UIView or UIBarButtonItem

// The list of items to display. Can be changed dynamically
dropDown.dataSource = ["Car", "Motorcycle", "Truck"]

Handle selection 手柄选择

// Action triggered on selection
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
  print("Selected item: \(item) at index: \(index)")
}

Handle show and hide 处理显示和隐藏

dropDown.show()
dropDown.hide()

very easy to customize 易于定制

Hope this will help you 希望这个能对您有所帮助

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

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