在 UIActionSheet 中将取消按钮放置在底部

5

在UIActionSheet中,我有一个UITableView和取消按钮。默认情况下,取消按钮出现在顶部。我想把它放在表格视图之后的底部。我该怎么做?

3个回答

6

对我来说这很有效:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"SomeTitle" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionSheet addButtonWithTitle:@"Some Action"];        
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons -1;

1
你说得对。如果你使用addButtonWithTitle:而不是使用初始化程序来处理所有内容,那么你需要指定取消按钮的索引。 - Kyle Clegg

0

Swift中的示例:

func presentMyActionSheetIOS7() {
    let actionSheet: UIActionSheet = UIActionSheet(title: "What do you want to do?", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)

    actionSheet.addButtonWithTitle("Change Transparency")
    actionSheet.addButtonWithTitle("Hide Photo")
    actionSheet.addButtonWithTitle("Cancel")
    actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1

    actionSheet.showInView(self.view)
}

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    switch buttonIndex {
        case 0:
            println("Change Transparency")
        case 1:
            println("Hide Photo")
        case 2:
            println("Cancel")
        default:
            println("Default")
    }
}

0

我尝试过这个,取消按钮在底部:

menu = [[UIActionSheet alloc] initWithTitle:@"Actionsheet"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:@"destructive"
                                         otherButtonTitles:@"other1", nil];
menu.actionSheetStyle = UIActionSheetStyleDefault;
[menu addButtonWithTitle:@"Cancel"];

默认情况下,取消按钮是隐藏的,添加取消将显示它。

但是:如果您在操作表上有其他GUI元素,则必须

选项1)将它们定位以隐藏其他按钮(为您的GUI元素腾出空间)。这是一个调整,但在某些情况下可以起作用。或者

选项2)您必须手动将按钮添加到操作表中。

操作表的内置按钮不能自由地对齐到底部,因为此内置GUI元素的目的不同。

请参见:将UIPickerView添加到UIActionSheet(按钮位于底部)


这个可以工作是因为你有其他几个按钮在取消按钮上面。但是在我的情况下,我没有其他的按钮,只有一个表格视图和取消按钮。 - Arun
最后我明白了你的设置,所以我认为你唯一的选择就是在你的操作表中添加一个UIButton。 - nzs
你需要确保的是(对我来说这是个问题),将你的uibutton放置在uiactionsheet画布(frame)上。如果没有这样做,uibutton的按压事件将无法正确分发。这可能是因为uiactionsheet的frame大小不足以正确容纳所有元素。 - nzs
如果需要自定义添加UIButtion,请查看我在此帖子中的SO链接...如果它对您有用,您可以接受我的答案 ;) - nzs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接