首先我們知道navigationBar的結構圖NavigationBar 結構圖,所以我們要做到這種效果,最終是要改變圖片的alpha。基于此有如下3種方法。
1.設置漸變圖片
根據上面設置為透明的方法,我們最直接能想到的還是setBackgroundImage,根據滑動距離去設置圖片的alpha。
可以根據圖片alpha和color封裝一個圖像類別如下:
+ (UIImage*)imageWithColor:(UIColor*)color{CGRectrect =CGRectMake(0.0f,0.0f,1.0f,1.0f);UIGraphicsBeginImageContext(rect.size);CGContextRefcontext =UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [colorCGColor]);CGContextFillRect(context, rect);UIImage*image =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnimage;}
2.根據運行時的特性,動態綁定可以參考動態綁定demo可參考GitHub - ltebean/LTNavigationBar: UINavigationBar Category which allows you to change its appearance dynamically
3.直接獲取NavigationBar子視圖ImageView,然后設置他的透明度.然后用一個全局的imageView引用他。從結構圖中可以看出來,他是NavigationBar的子視圖,我們可以通過for...in循環遍歷navigationBar.subviews,然后獲得這個view.或者直接直接獲取UIImage = self.navigationController.navigationBar.subviews.firstObject;
第一次寫博文,若有不妥之處,望賜教!