UIView子类在UIViewController +故事板在Swift中设置自己的高度+约束
我有一个UIViewController子类包含UITableView,UIView的一个子类称为ICYCollapsableInputView,另一个UIView子类称为ICYHeaderVIew。UIView子类在UIViewController +故事板在Swift中设置自己的高度+约束
ICYHeaderView是屏幕截图中显示的四舍五入UIView。它包含一个圆形按钮 - 带有绿色“添加”(+)按钮的按钮。
ICYCollapsableInputView是一个现在具有UITextField和Save按钮的视图。我希望能够通过调用ICYCollapsableInputView的适当函数来从UIViewController中展开和折叠此视图。
当用户点击ICYHeaderView上的圆形按钮时,Ithe 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)
}
}
所有你需要IA只是改变了高度的限制。
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
Trailaing空间,上海华 - 0,领先的空间,上海华 - 0,上海华顶空 - 0,底部的空间来实现代码如下 - 0
为TableView中
到inputVw顶部空间 - 0,导致空间上海华 - 0,尾随空间超视图 - 0,底部空间superview - 0
您是否找到解决方案?我有同样的问题。这是否发生是因为视图正在改变自己的身高? –