【发布时间】:2017-06-15 12:26:25
【问题描述】:
我有一个视图,我正在通过 xib 以编程方式在添加按钮上创建另一个视图。我可以在点击添加更多按钮时创建多个视图,如果我删除最后一个视图,删除也可以工作,但是由于缺少约束视图没有正确更新,中间视图出现问题下面是图像的外观
在开始视图看起来像这样
添加更多视图后
移除中间视图后
删除按钮代码
@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])
}
委托改变superview的高度
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
}
}
}
这里是演示项目的链接 https://github.com/logictrix/addFieldDemo
【问题讨论】:
-
使用 UITabelView。您可以非常轻松地实现这种类型的操作。
-
@Ujesh 是的,如果我无法解决这个问题,我必须使用其他选项,现在我正在尝试解决这个问题。
-
这个当然可以修复,但是最好使用TableView,如果这是无法使用TableViews的作业,可以理解。否则,请始终使用 Apple 为您提供的最佳工具。
-
@CoderFrom94 这不是分配,但如果我使用 tableview,我必须从一开始就为整个 ViewController 设计和编码,如果我可以通过一些帮助快速解决这个问题,我不想这样做
-
TableView 对于 full 界面可能是一个好主意,但是尝试为该部分添加 TableView 将很难工作(内部的垂直滚动表格视图一个垂直滚动的滚动视图)。
标签: ios swift swift3 nslayoutconstraint