[英]How to add Cancel button between two other buttons (stacked) in UIAlertView (iOS)
我試圖用三個按鈕(將被堆疊)創建一個UIAlertView。 我希望“取消”按鈕位於其他兩個按鈕之間。 我曾嘗試將cancelButtonIndex設置為1,但是如果還有其他兩個按鈕,它只會將它們放置在索引0和1上。我知道我可以更改按鈕的名稱,但是我想取消按鈕的顏色為深藍色。
編輯:**請注意-我知道如何以正確的順序獲取帶有標題的三個按鈕,但前提是所有三個按鈕本質上看起來都像“其他”按鈕; 我希望取消按鈕具有深藍色背景的取消按鈕,以便看起來像常規的取消按鈕。 **
我試過了
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:button1Title,button2Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert show];
和
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:button2Title];
[alert show];
和
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:addButtonWithTitle:button1Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button2Title];
[alert show];
無濟於事。 甚至有可能實現我想做的事情?
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert show];
可能有幫助,
干杯。
對於這個答案,我有兩個輔助要點。
1)據我所知,Apple並未拒絕對UIAlertView
進行合理修改的應用程序; 他們說過,像UIAlertView
這樣的類的視圖層次結構應被視為私有的。
2)這個問題很好地說明了為什么您應該問更多關於最終目標的問題,而不是要達到目標的步驟。 我知道這個問題的唯一原因是我在這里的答案中留下了評論。
回答:
由於您的評論,我知道您希望創建一個UIAlertView
,該UIAlertView
具有堆疊的按鈕,即使只有2個按鈕也是如此。
我發現這樣的代碼最合理的位置是在類別中。 由於通常需要對警報視圖進行操作的代碼必須圍繞show
調用,因此我創建了我調用的類別方法而不是show
而該方法又調用了show
本身。
-(void)showWithButtonsStacked{
static NSString *tempButtonTitle = @"SomeUnlikelyToBeUsedTitle";
BOOL willAddFakeButton = (self.numberOfButtons == 2); // Button are only side by side when there's 2
if (willAddFakeButton){
self.clipsToBounds = YES;
[self addButtonWithTitle:tempButtonTitle]; // add temp button so the alertview will stack
}
BOOL hasCancelButton = (self.cancelButtonIndex != -1); // If there is a cancel button we don't want to cut it off
[self show];
if (willAddFakeButton){
UIButton *cancelButton = nil;
UIButton *tempButton = nil;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (hasCancelButton && [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){
cancelButton = button;
} else if ([button.titleLabel.text isEqualToString:tempButtonTitle]) {
tempButton = button;
}
}
}
if (hasCancelButton){ // move in cancel button
cancelButton.frame = tempButton.frame;
}
[tempButton removeFromSuperview];
// Find lowest button still visable.
CGRect lowestButtonFrame = CGRectZero;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (button.frame.origin.y > lowestButtonFrame.origin.y){
lowestButtonFrame = button.frame;
}
}
}
// determine new height of the alert view based on the lowest button frame
CGFloat newHeight = CGRectGetMaxY(lowestButtonFrame) + (lowestButtonFrame.origin.x * 1.5);
self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);
}
}
此方法完成其目標的方式是向警報視圖添加一個臨時按鈕,以強制警報視圖堆疊按鈕,然后刪除該臨時按鈕並調整高度。 由於它是一個類別方法,因此只需調用以下命令即可使用它:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test title" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert showWithButtonsStacked];
此代碼會產生如下警告:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert setCancelButtonIndex:1]; // to make it look like cancel button
[alert show];
將取消按鈕設置為nil
然后將其添加到其他按鈕中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.