Ios UIView子类设置自己的高度+;UIViewController中的约束+;斯威夫特的故事板

Ios UIView子类设置自己的高度+;UIViewController中的约束+;斯威夫特的故事板,ios,swift,uitableview,uiview,subclass,Ios,Swift,Uitableview,Uiview,Subclass,我有一个UIViewController子类,它包含一个UITableView、一个名为ICYCollapsableInputView的UIView子类和另一个名为ICYHeaderVIew的UIView子类 ICYHeaderView是屏幕截图中显示的四舍五入的UIView。它包含一个圆形按钮-带有绿色“添加”(+)按钮的按钮 ICYCollapsableInputView是一个视图,它现在将有一个UITextField和一个Save按钮。我希望能够通过调用ICYCollapsableInpu

我有一个UIViewController子类,它包含一个UITableView、一个名为ICYCollapsableInputView的UIView子类和另一个名为ICYHeaderVIew的UIView子类

ICYHeaderView是屏幕截图中显示的四舍五入的UIView。它包含一个圆形按钮-带有绿色“添加”(+)按钮的按钮

ICYCollapsableInputView是一个视图,它现在将有一个UITextField和一个Save按钮。我希望能够通过调用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)

}

 }

你所需要的只是改变高度限制

    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)
}
故事板中的约束应该遵循

用于输入vw

跟踪空间到superview-0,引导空间到superview-0,顶部空间到superview-0,底部空间到tableview-0

对于TableView


顶部空间输入VW-0,前导空间输入superview-0,尾随空间输入superview-0,底部空间输入superview-0,您只需更改高度约束即可

    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)
}
故事板中的约束应该遵循

用于输入vw

跟踪空间到superview-0,引导空间到superview-0,顶部空间到superview-0,底部空间到tableview-0

对于TableView


顶部空间输入VW-0,前导空间输入superview-0,尾部空间输入superview-0,底部空间输入superview-0,我已经尝试过了。除了ICYCollapsableInputView的主框架(红色部分)没有改变之外,它也做了同样的事情。其中是高度约束??我在ICYCollapsableInputView xib上设置了高度约束。您可以在图2中看到它。我想补充一点,我知道我可以在视图控制器上设置高度约束(这是可行的),但我想封装ICYCollapsableInputView,并让它根据我添加到其中的子视图设置自己的高度-如果可能的话。我已经尝试过了。除了ICYCollapsableInputView的主框架(红色部分)没有改变之外,它也做了同样的事情。其中是高度约束??我在ICYCollapsableInputView xib上设置了高度约束。您可以在图2中看到它。我想补充一点,我知道我可以在视图控制器上设置高度约束(这是可行的),但我想封装ICYCollapsableInputView,并让它根据我添加到其中的子视图设置自己的高度-如果可能的话。找到解决方案了吗?我也有同样的问题。发生这种情况是因为视图改变了自身的高度吗?您找到解决方案了吗?我也有同样的问题。是否因为视图正在更改其自身的高度而发生这种情况?