UIView子类在Swift中的UIViewController + Storyboard中设置自己的高度+约束

时间:2017-04-02 00:53:54

标签: ios swift uitableview uiview subclass

我有一个UIViewController子类,它包含一个UITableView,一个名为ICYCollapsableInputView的UIView的子类,以及另一个名为ICYHeaderVIew的UIView子类。

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

ICYCollapsableInputView是一个现在具有UITextField和Save按钮的视图。我希望能够通过调用ICYCollapsableInputView的相应函数从UIViewController展开和折叠此视图。

当用户点击ICYHeaderView上的圆形按钮时,ICYCollapsableInputView应该在高度上扩展,按下UITableView。我有ICYCollapsableInputView扩展和折叠,但我无法弄清楚如何让UITableView响应更改。 (见动画)

expand/collapse animation

我使用红色背景为xib中的主视图着色,并在其中添加了蓝色背景的视图(参见屏幕截图)

enter image description here

我可以使用自己的高度约束来调整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)

}

 }

1 个答案:

答案 0 :(得分: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)
}

故事板中的约束应该遵循

表示inputVW

Trailaing space to superview - 0,领先空间到superview - 0,top space to superview - 0,bottom space to tableview - 0

for TableView

顶部空间inputVw - 0,前导空间到superview - 0,尾随空间到超级视图 - 0,底部空间到superview - 0