[英]UIView Subclass Set Own Height + Constraints In UIViewController + Storyboard in Swift
I have a UIViewController subclass that contains a UITableView, a subclass of UIView called ICYCollapsableInputView, and another UIView subclass called ICYHeaderVIew. 我有一个UIViewController子类,它包含一个UITableView,一个名为ICYCollapsableInputView的UIView的子类,以及另一个名为ICYHeaderVIew的UIView子类。
ICYHeaderView is the rounded off looking UIView shown in the screenshots. ICYHeaderView是截图中显示的圆形外观UIView。 It contains a round button - the one with the green "add" (+) button.
它包含一个圆形按钮 - 带有绿色“添加”(+)按钮的按钮。
ICYCollapsableInputView is a view that will for now have a UITextField and a Save button. ICYCollapsableInputView是一个现在具有UITextField和Save按钮的视图。 I want to be able to expand and collapse this view from the UIViewController by calling the appropriate functions of the ICYCollapsableInputView.
我希望能够通过调用ICYCollapsableInputView的相应函数从UIViewController展开和折叠此视图。
When the user taps the round button on the ICYHeaderView, Ithe ICYCollapsableInputView is supposed to expand in height, pushing down the UITableView. 当用户点击ICYHeaderView上的圆形按钮时,ICYCollapsableInputView应该在高度上扩展,按下UITableView。 I have the ICYCollapsableInputView expanding and collapsing, but I can't figure out how to get the UITableView to respond to the changes.
我有ICYCollapsableInputView扩展和折叠,但我无法弄清楚如何让UITableView响应更改。 (See Animation)
(见动画)
I colored the main view in the xib with a RED background and the view I added in it with a BLUE background (see screenshot) 我用红色背景为xib中的主视图着色,并在其中添加了蓝色背景的视图(参见屏幕截图)
I can resize the ICYCollapsableInputView (as seen in the code and animation) using its own height constraint, but the UITableView constraint seems to ignore it. 我可以使用自己的高度约束来调整ICYCollapsableInputView(如代码和动画中所示),但是UITableView约束似乎忽略它。
Here is the code from ICYCollapsableInputView: 以下是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)
}
}
All you need ia just changing the height constraints. 所有你需要的只是改变高度限制。
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)
}
And Constraints in Storyboard should be following 故事板中的约束应该遵循
for inputVW 对于inputVW
Trailaing space to superview - 0 , leading space to superview - 0, top space to superview - 0 , bottom space to tableview - 0 Trailaing space to superview - 0,Leading space to superview - 0,top space to superview - 0,bottom space to tableview - 0
for TableView 对于TableView
top space to inputVw - 0, leading space to superview - 0, trailing space to super view - 0, bottom space to superview - 0 顶部空间输入Vw - 0,前导空间到超级视图 - 0,尾部空间到超级视图 - 0,底部空间到超级视图 - 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.