2017-04-02 55 views
1

我有一個UIViewController子類包含UITableView,UIView的一個子類稱爲ICYCollapsableInputView,另一個UIView子類稱爲ICYHeaderVIew。UIView子類在UIViewController +故事板在Swift中設置自己的高度+約束

ICYHeaderView是屏幕截圖中顯示的四捨五入UIView。它包含一個圓形按鈕 - 帶有綠色「添加」(+)按鈕的按鈕。

ICYCollapsableInputView是一個現在具有UITextField和Save按鈕的視圖。我希望能夠通過調用ICYCollapsableInputView的適當函數來從UIViewController中展開和摺疊此視圖。

當用戶點擊ICYHeaderView上的圓形按鈕時,Ithe 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) 

} 

} 
+0

您是否找到解決方案?我有同樣的問題。這是否發生是因爲視圖正在改變自己的身高? –

回答

0

所有你需要IA只是改變了高度的限制。

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空間,上海華 - 0,領先的空間,上海華 - 0,上海華頂空 - 0,底部的空間來實現代碼如下 - 0

爲TableView中

到inputVw頂部空間 - 0,導致空間上海華 - 0,尾隨空間超視圖 - 0,底部空間superview - 0

+0

我試過了。除了ICYCollapsableInputView的主框架(紅色部分)沒有改變之外,它做同樣的事情。 – Tom

+0

在圖像3中 - 約束條件中顯示4的4。高度約束在哪裏? –

+0

我在ICYCollapsableInputView xib上設置了高度約束。你可以在圖像2中看到它。 – Tom

相關問題