我有一個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)
}
}
您是否找到解決方案?我有同樣的問題。這是否發生是因爲視圖正在改變自己的身高? –