2010-08-04 87 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