2010-08-04 51 views
2

我想改變uialertview的背景。 一切工作正常,除了我添加到uialert視圖的背景圖像不會在alertview內縮放。所以基本上,你只能看到基於當前尺寸的背景圖像的一部分。 有沒有什麼辦法可以讓圖像在alerview內縮放直到合適?iphone ==> UIAlertView更改背景?

UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:title 
                message:msg 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles: nil]; 
    [theAlert show]; 

    UIImage *theImage = [UIImage imageNamed:@"bg.jpg"]; 
    theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16]; 
    CGSize theSize = [theAlert frame].size; 

    UIGraphicsBeginImageContext(theSize); 
    [theImage drawInRect:CGRectMake(0, 0, theAlert.frame.size.width, theAlert.frame.size.height)]; 
    theImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    theAlert.layer.contents = (id)[theImage CGImage]; 
    [theAlert release]; 

回答

5

請看看: http://joris.kluivers.nl/iphone-dev/?p=CustomAlert

他們解釋如何通過繼承UIAlertView中做到這一點。祝你好運!

+0

感謝很多很好的鏈接 – aryaxt 2010-08-05 00:52:31

+0

不適用於iOS 4.2 ... – Underdog 2011-03-01 11:14:17

+0

在iOS 6.1上運行很好,但不支持不同的界面方向(警報將只在縱向模式下)。我在談論這個鏈接:http://joris.kluivers.nl/blog/2012/03/02/custom-popups-revisited/ 如果你想實現不同的方向支持 - 你可以繼承UIViewController,使用相同的'window'方法只是改變'[window addSubview:]'window.rootViewController = ...的方法' – derpoliuk 2013-03-26 12:49:56