[iOS]實現(xiàn)UINavigationBar背景透明但是item不透明的一種方法

剛剛看了 iOS --沒事擼一擼控件之UINavigationBar 這篇文章, 其中提到了 UINavigationBar的滑動漸變效果和去除底部橫線的方法, 深有其感, 現(xiàn)提供另一種簡單的方法來實現(xiàn).

其實也沒有什么 special 啦, 就是通過顏色來生成一個UIImage, 設(shè)置成背景圖片即可.

通過顏色來生成一張純色的圖片:

func imageFromColor(color:UIColor)->UIImage{
 let rect = CGRectMake(0, 0, 1, 1)
 UIGraphicsBeginImageContext(rect.size);
 let context = UIGraphicsGetCurrentContext();
 CGContextSetFillColorWithColor(context, color.CGColor)
 CGContextFillRect(context, rect)
 let image = UIGraphicsGetImageFromCurrentImageContext()
 UIGraphicsEndImageContext()
 return image
 }

使用的時候:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let random = CGFloat(arc4random() % 9) / 10
        let color = UIColor(red: 0, green: 120 / 255, blue: 106 / 255, alpha: random)
        let a = NSDate().timeIntervalSince1970
        self.navigationController?.navigationBar.setBackgroundImage(imageFromColor(color), forBarMetrics: UIBarMetrics.Default)
        print(NSDate().timeIntervalSince1970 - a)//0.00041508674621582
    }

效果圖:

至于去除底部那一條難看的橫線, 恩...其實一行代碼就搞定了的....那就是:

self.navigationController?.navigationBar.shadowImage = UIImage()

效果圖:

預(yù)祝各位挖坑愉快.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容