我有一个UIViewController子类,它包含一个UITableView、一个UIView的子类icycollappableinputview和另一个UIView子类ICYHeaderVIew。
ICYHeaderView是截图中显示的外观完整的UIView。它包含一个圆形按钮,即带有绿色“add”(+)按钮的按钮。
icycollappableinputview是一个视图,它现在有一个UITextField和一个Save按钮。我希望能够通过调用icycollappableinputview的相应函数从UIViewController中展开和折叠此视图。
当用户点击ICYHeaderView上的圆形按钮时,ICYCollapsableInputView应该在高度上展开,并按下UITableView。我已经扩展和折叠了icycollappableinputview,但是我不知道如何让UITableView响应更改。(请参见动画)
我用红色背景将XIB中的主视图着色,并用蓝色背景添加了视图(见屏幕截图)。
我可以使用icycollappableinputview自身的高度约束调整其大小(如代码和动画中所示),但UITableView约束似乎忽略了它。
以下是icycollappableinputview中的代码:
class ICYCollapsableInputView: UIView {
var view: UIView!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBInspectable public var collapsedHeight: CGFloat = 150 {
didSet {
self.heightConstraint.constant = collapsedHeight
}
}
@IBInspectable public var expandedHeight: CGFloat = 250 {
didSet {
self.heightConstraint.constant = expandedHeight
}
}
// MARK: - Init
func loadFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "ICYCollapsableInputView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
override init(frame: CGRect) {
// 1. setup any properties here
// 2. call super.init(frame:)
super.init(frame: frame)
// 3. Setup view from .xib file
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
// 1. setup any properties here
// 2. call super.init(coder:)
super.init(coder: aDecoder)
// 3. Setup view from .xib file
xibSetup()
}
func xibSetup() {
view = loadFromNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
self.heightConstraint.constant = self.collapsedHeight
var rect = self.frame
rect.size.height = self.collapsedHeight
self.frame = rect
self.view.frame = rect
}
func revealInputView() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
var rect = self.frame
rect.size.height = self.expandedHeight
self.frame = rect
self.heightConstraint.constant = self.expandedHeight
self.view.layoutIfNeeded()
}, completion: nil)
}
func closeInputView() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
self.heightConstraint.constant = self.collapsedHeight
var rect = self.frame
rect.size.height = self.collapsedHeight
self.frame = rect
self.view.layoutIfNeeded()
}, completion: nil)
}
}
最佳答案
你只需要改变高度限制。
func revealInputView() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
self.heightConstraint.constant = self.expandedHeight //or 250
self.view.layoutIfNeeded()
}, completion: nil)
}
func closeInputView() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
self.heightConstraint.constant = self.collapsedHeight //or 150
self.view.layoutIfNeeded()
}, completion: nil)
}
故事板中的约束应该是
对于inputVW
将空格拖至superview-0,将空格拖至superview-0,顶部空格拖至superview-0,底部空格拖至tableview-0
对于TableView
顶部空间到inputVw-0,引导空间到super view-0,尾部空间到superview-0,底部空间到superview-0