Skip to content

Instantly share code, notes, and snippets.

@oksep
Last active June 15, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oksep/d4c5ee93323fc42d8c5db677621c9886 to your computer and use it in GitHub Desktop.
Save oksep/d4c5ee93323fc42d8c5db677621c9886 to your computer and use it in GitHub Desktop.
UINavigationBar transparency by ScrollView
/**
* 可渐变的 UINavigationBar
* 从 objective-c 版本 https://github.com/ltebean/LTNavigationBar 翻译而来
*/
extension UINavigationBar {
@nonobjc static var overlay:UIView?
// 设置 overlay 背景色
func lt_setBackgroundColor(backgroundColor: UIColor) {
if UINavigationBar.overlay == nil {
self.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
UINavigationBar.overlay = UIView(frame: CGRectMake(0, -20, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + 20))
UINavigationBar.overlay!.userInteractionEnabled = false
// let mask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
UINavigationBar.overlay!.autoresizingMask = UIViewAutoresizing.FlexibleWidth
UINavigationBar.overlay!.autoresizingMask = UIViewAutoresizing.FlexibleHeight
self.insertSubview(UINavigationBar.overlay!, atIndex: 0)
}
UINavigationBar.overlay?.backgroundColor = backgroundColor
}
// 设置 UINavigationItem alpha 通道
func lt_setElementsAlpha(alpha: CGFloat) {
self.valueForKey("_leftViews")?.enumerateObjectsUsingBlock({ (view:AnyObject, p: UnsafeMutablePointer<ObjCBool>) in
(view as? UIView)?.alpha = alpha
})
self.valueForKey("_rightViews")?.enumerateObjectsUsingBlock({ (view:AnyObject, p: UnsafeMutablePointer<ObjCBool>) in
(view as? UIView)?.alpha = alpha
})
let titleView = self.valueForKey("_titleView") as? UIView
titleView?.alpha = alpha
// when viewController first load, the titleView maybe nil
for v in self.subviews {
if v.isKindOfClass(UINavigationItem) {
(v as UIView).alpha = alpha
break
}
}
}
// 设置 UINavigationBar偏移量
func lt_setTranslationY(translationY: CGFloat) {
self.transform = CGAffineTransformMakeTranslation(0, translationY);
}
// 重置
func lt_reset() {
self.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
UINavigationBar.overlay?.removeFromSuperview()
UINavigationBar.overlay = nil;
}
}
// tableView 交互
extension UIViewController: UIScrollViewDelegate {
@nonobjc static let NAVBAR_CHANGE_POINT: CGFloat = CGFloat(50)
// 监听滚动
func scrollViewDidScroll(scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let alpha: CGFloat
if (offsetY > DetailViewController.NAVBAR_CHANGE_POINT) {
alpha = min(1, 1 - ((DetailViewController.NAVBAR_CHANGE_POINT + 64 - offsetY) / 64))
} else {
alpha = 0
}
let color = UIColor(red:0/255.0, green:175/255.0, blue:240/255.0, alpha:1).colorWithAlphaComponent(alpha)
self.navigationController?.navigationBar.lt_setBackgroundColor(color)
}
override func viewWillAppear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
tableView.delegate = self
self.scrollViewDidScroll(tableView)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
tableView.delegate = nil
self.navigationController?.navigationBar.lt_reset()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment