繁体   English   中英

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

[英]UIView Subclass Set Own Height + Constraints In UIViewController + Storyboard in Swift

我有一个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)
}

故事板中的约束应该遵循

对于inputVW

Trailaing space to superview - 0,Leading space to superview - 0,top space to superview - 0,bottom space to tableview - 0

对于TableView

顶部空间输入Vw - 0,前导空间到超级视图 - 0,尾部空间到超级视图 - 0,底部空间到超级视图 - 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM