簡體   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