iOS開發中的那些小技巧,iOS開發小技巧

來源:互聯網
上載者:User

iOS開發中的那些小技巧,iOS開發小技巧
前言:今天在寫代碼的過程中遇到一個需要修改系統navigationBar的背景色,我起初用的是barTintColor去修改但是防不住系統點擊按鈕的時候會有一個渲染高亮的效果,調了好久沒有達到自己想要的效果,最後放棄用顏色來搞這個了,看了一下swift的API發現也可以用圖片,有不好意思找UI(自己的demo找UI切圖不太合適,只好來把顏色改成圖片了)

本文開始啦:

先上一份OC的代碼吧

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

swift代碼奉上

func imageWithColor(color:UIColor) -> (UIImage) {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor)
context!.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return image!
}
剛剛接觸swift不是特別久如果大神有更好的方法,還請不吝賜教,轉載請附上原地址(不附上也沒事^_^)
謝謝!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.