[英]Swift Constraints & Anchors
实际上,我在寻找一个下拉按钮的示例。 但是不幸的是,事实证明它不能完全正常工作,我也不知道动态约束和锚点是如何工作的。 因此,我要求您的帮助。
“ NSGenericException”,原因:“无法通过以下方式激活约束
锚,因为他们没有共同的祖先。 约束或其锚点是否引用了不同视图层次结构中的项目? 那是非法的。
您可以在Github上找到我的源代码: https : //github.com/Archetapp/Drop-Down-Menu/blob/master/sdafjkbslib/ViewController.swift
我发现了类似的问题,他们的解决方案是:
在激活约束之前,请检查是否已将以编程方式创建的视图添加到其父视图。
检查是否在viewDidLoad()/ viewWillAppear()内部编写约束激活代码。 您应该在updateViewConstraints或viewWillLayoutSubviews中编写约束。
检查是否关闭了translatesAutoresizingMaskIntoConstraints。
但这并没有帮助我。
对于下拉菜单,我最喜欢此库的代码数量少且易于处理
初始化
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"]
手柄选择
// Action triggered on selection
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
}
处理显示和隐藏
dropDown.show()
dropDown.hide()
易于定制
希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.