2017-07-26 85 views
0

如何使用動畫增加iMessage(iOS 10)中的導航欄高度,以及何時隱藏此控制器,然後還使用動畫返回之前的高度?iMessage(iOS 10)中的導航欄高度

我讀過幾篇文章,我可以在這裏分享,但是他們沒有提供iMessage(iOS 10)中的效果。謝謝。

更新:或者如何跟蹤該UIViewController是隱藏的點做一個平滑的動畫,因爲我們得到了viewWillDisappear呼叫控制器開始隱藏,但它也可能是用戶使用其隱藏緩慢的手勢,因此有必要減少相對於當前控制器已經隱藏多少百分比的高度。實施它有什麼好處?

回答

1

您可以使用動畫對導航欄的高度進行更改,方法是對其進行子類化並創建高度屬性(barHeight)並在設置值之後爲其設置動畫效果。

斯威夫特3

final class CustomHeightNavigationBar: UINavigationBar { 

var navigationItemsOffset: CGPoint = CGPoint(x: 0, y: 10) { // default offset (below statusbar) 
    didSet { 
     UIView.animate(withDuration: 0.25) { [weak self] in 
      self?.setNeedsLayout() 
     } 
    } 
} 

var barHeight: CGFloat = 60 { // default height 
    didSet { 
     UIView.animate(withDuration: 0.25) { [weak self] in 
      self?.sizeToFit() 
      self?.setNeedsLayout() 
     } 
    } 
} 

override func sizeThatFits(_ size: CGSize) -> CGSize { 
    return CGSize(width: UIScreen.main.bounds.size.width, height: barHeight) 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 

    frame.origin = navigationItemsOffset 

    subviews.forEach { (subview) in 
     subview.center.y = center.y 
    } 
} 

創建你已經將其設置爲在故事板的導航欄,它位於導航控制器的自定義類子類之後。

enter image description here

現在,你可以通過改變barHeight屬性的值進行動畫視圖控制器中的導航欄的高度。

斯威夫特3

var navigationBar: CustomHeightNavigationBar? { 
    guard let navigationBar = navigationController?.navigationBar as? CustomHeightNavigationBar else { 
     return nil 
    } 
    return navigationBar 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    if let navigationBar = navigationBar { 
     navigationBar.barHeight = 60 
    } 
} 

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 
    if let navigationBar = navigationBar { 
     navigationBar.barHeight = 44 
    } 
} 
0

我想你正在尋找的是這樣的: 爲了在完全相同的時間過渡到動畫欄的高度,你必須實現這個方法on viewWillAppear and viewWillDisappear

 self.transitionCoordinator?.animate(alongsideTransition: { (context) in 
     let height: CGFloat = 80 //any size you want 
     let bounds = self.navigationController!.navigationBar.bounds 
     self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: height) 

    }, completion: { (context) in 
     //Any code you may want to add after the transition 
    })