[英]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.