我有一个 View ,其中我通过 xib 在添加按钮上以编程方式创建另一个 View 。我可以在点击添加更多按钮时创建多个 View ,如果我删除最后一个 View ,则删除也可以正常工作,但是由于缺少约束,中间 View 会出现问题 View 未正确更新下面是图像的外观
在开始 View 看起来像这样
添加更多 View 后
移除中间 View 后
删除按钮代码
@IBAction func deletebnt(_ sender: UIButton) {
let view = self.superview
let index = view?.subviews.index(of:self)!
delegate.txtcheck(text: countstr)
self.view.removeFromSuperview()
}
添加按钮代码
@IBAction func addMoreBnt(_ sender: UIButton) {
for constraint in addSuperview.constraints {
if constraint.firstAttribute == NSLayoutAttribute.height
{
constraint.constant += 45
space = constraint.constant
}
}
let newView : AvalabileTimeView = AvalabileTimeView()
newView.frame = CGRect(x: self.addsubView.frame.origin.x, y: 70, width: addsubView.frame.size.width, height:addsubView.frame.size.height)
newView.delegate = self as AvalabileTimeDelegate
addSuperview.addSubview(newView)
let index = addSuperview.subviews.index(of: newView)!
newView.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = newView.widthAnchor.constraint(equalToConstant:addsubView.frame.size.width )
let widthConstaint = newView.heightAnchor.constraint(equalToConstant:31 )
let topConstraint = newView.topAnchor.constraint(equalTo: addSuperview.topAnchor, constant: space - 31) NSLayoutConstraint.activate([heightConstraint,topConstraint,widthConstaint])
}
委托(delegate)改变父 View 的高度
func txtcheck(text: String!) {
print(text)
for constraint in addSuperview.constraints {
if constraint.firstAttribute == NSLayoutAttribute.height
{
constraint.constant -= 45
// Here I have to set constraint for bottom view and topview of deleted view but I don't know how to do
}
}
}
最佳答案
不是添加每个新的 AvalabileTimeView
都有自己的约束,而是使用 UIStackView
- 您可以删除几乎所有用于添加/删除新 View 的现有代码。
查看.addArrangedSubview()
和.removeArrangedSubview()
这是一些示例代码...您需要在 Interface Builder 中添加一个 UIStackView,将其连接到 Outlet,并调整约束,仅此而已:
// in ViewController.swift
@IBOutlet weak var availableTimeStackView: UIStackView!
@IBAction func addMoreBnt(_ sender: UIButton) {
// instantiate a new AvalabileTimeView
let newView : AvalabileTimeView = AvalabileTimeView()
// set its delegate to self
newView.delegate = self as AvalabileTimeDelegate
// add it to the Stack View
availableTimeStackView.addArrangedSubview(newView)
// standard for auto-layout
newView.translatesAutoresizingMaskIntoConstraints = false
// only constraint needed is Height (width and vertical spacing handled by the Stack View)
newView.heightAnchor.constraint(equalToConstant: 31).isActive = true
}
// new delegate func
func removeMe(_ view: AvalabileTimeView) {
// remove the AvalabileTimeView from the Stack View
availableTimeStackView.removeArrangedSubview(view)
}
// in AvalabileTimeView.swift
protocol AvalabileTimeDelegate{
// don't need this anymore
func txtcheck(text: String!)
// new delegate func
func removeMe(_ view: AvalabileTimeView)
}
@IBAction func deletebnt(_ sender: UIButton) {
delegate.removeMe(self)
}
关于ios - 如何删除 View 和更新约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44567504/