【发布时间】:2017-04-02 00:53:54
【问题描述】:
我有一个 UIViewController 子类,其中包含一个 UITableView、一个名为 ICYCollapsableInputView 的 UIView 子类和另一个名为 ICYHeaderVIew 的 UIView 子类。
ICYHeaderView 是屏幕截图中显示的四舍五入的 UIView。它包含一个圆形按钮 - 带有绿色“添加”(+) 按钮的按钮。
ICYCollapsableInputView 是一个视图,现在将有一个 UITextField 和一个保存按钮。我希望能够通过调用 ICYCollapsableInputView 的适当函数从 UIViewController 展开和折叠此视图。
当用户点击 ICYHeaderView 上的圆形按钮时,ICYCollapsableInputView 应该会在高度上展开,向下推 UITableView。我有 ICYCollapsableInputView 展开和折叠,但我不知道如何让 UITableView 响应更改。 (见动画)
我用红色背景为 xib 中的主视图着色,我在其中添加的视图用蓝色背景着色(见屏幕截图)
我可以使用自己的高度约束来调整 ICYCollapsableInputView(如代码和动画中所示)的大小,但 UITableView 约束似乎忽略了它。
这是来自 ICYCollapsableInputView 的代码:
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)
}
}
【问题讨论】:
-
您找到解决方案了吗?我有同样的问题。发生这种情况是因为视图正在改变自己的高度吗?
标签: ios swift uitableview uiview subclass