2017-07-26 87 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 
    })