2012-01-07 70 views
1

下來,我有這顯示在橫向模式下的應用程序,我已經覆蓋一個UIAlertView的高度,用下面的代碼:移動按鈕UIAlertView中

- (void) willPresentAlertView:(UIAlertView *)alertView 
{ 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
    CGRect frame = [alertView frame]; 
    alertView.frame = CGRectMake(frame.origin.x, 0, frame.size.width, screenBounds.size.width); 
} 

這幾乎工程。 UIAlertView更高,但是,alertview中的按鈕不會被推下。

有沒有人知道我如何在UIAlertView中按下按鈕時,當我更改警報視圖的高度?

回答

0

我猜你可能不得不繼承/操作UIAlertView類來實現這一點,這可能是有風險的,因爲Apple可以改變它們的實現,以便在適當的檢查中包裝你的代碼。

一開始是做:

NSLog(@"UIAlertView subviews: %@",alertView.subviews); 

這會打印一些輸出組成UIAlertView中的各種元素,你可能會看到一些UIButtons在那裏,你可以然後通過使用一些操縱像:

UIButton* button1 = (UIButton*)[alertView.subviews objectAtIndex:N]; 
[button1 setFrame:CGRect(button1.frame.origin.x, button1.frame.origin.y+10, button1.frame.size.width, button1.frame.size.height)] 

一個明智的檢查將是確認objectAtIndex是正確類型的元素,你就可以進行操作之前,這是不是萬無一失但是蘋果可能會增加更多的UIButtons或東西..

if ([[alertView.subviews objectAtIndex:N] isKindOfClass:[UIButton class]]) { 
    ... 
} 

根據你的情況,你可能還需要遍歷數組子視圖和移動所有UIButtons下來,而不是僅僅在某些特定objectsAtIndex硬編碼但這完全是另外一個答案。 :-)

+0

有效點,但它可能會導致App Store由於與Alert View類混淆而被拒絕。順便說一句,愛用戶名。 – DGund 2012-01-07 22:28:25

+1

同意改變它們是有風險的,但既然你已經提到過,我認爲我會提供一個可能的方式來做到這一點。你永遠不知道,也許這是一個封閉的版本:-)。 – GregularExpressions 2012-01-07 22:31:39

+0

您應該將此添加到您的答案中,因爲我花了大約一個小時才弄清楚了這一點:爲子類UIAlertView重置按鈕的框架時,必須將其重置爲類的'layoutSubviews'方法,而不是'initWithTitle ...'方法。 – w3bshark 2013-03-11 20:06:44

0

This link應該有所幫助。在我看來,我不會嘗試搞亂視圖層次結構。這可能會導致從App Store中被拒絕。無論是從頭構建一個新的AlertView,還是保持原樣。

0

的NSArray * subViewArray = [alertView子視圖]。

for (NSUInteger ind=0 ; ind < [subViewArray count]; ind++) { 

    if ([[alertView.subviews objectAtIndex:ind] isKindOfClass:[UIButton class]]) { 
     UIButton* button1 = (UIButton*)[alertView.subviews objectAtIndex:ind]; 
     [button1 setFrame:CGRectMake(button1.frame.origin.x, button1.frame.origin.y+100, button1.frame.size.width, button1.frame.size.height)]; 
     NSLog(@"button1.frame.origin.y=[%1.0f]",button1.frame.origin.y); 

    } 
}