給導航條設置成顏色的背景圖像 : UIGraphicsBeginImageContext ...

self.navigationBar.setBackgroundImage(createImageWithColor(UIColor.clear), for: .default)


//////////////////////


func createImageWithColor(_ color:UIColor) -> UIImage{

    return createImageWithColor(color, size: CGSize(width: 1, height: 1))

}

func createImageWithColor(_ color:UIColor,size:CGSize) -> UIImage {

    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)

    UIGraphicsBeginImageContext(rect.size);

    let context = UIGraphicsGetCurrentContext();

    context?.setFillColor(color.cgColor);

    context?.fill(rect);

    

    let theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return theImage!;

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章