Ios UIalertcontroller按钮顺序
我想更改警报操作按钮的顺序,根据代码,我尝试了所有的可能性,但不允许我更改顺序 根据HIG,取消位于右侧 看到我的代码了吗Ios UIalertcontroller按钮顺序,ios,uialertcontroller,Ios,Uialertcontroller,我想更改警报操作按钮的顺序,根据代码,我尝试了所有的可能性,但不允许我更改顺序 根据HIG,取消位于右侧 看到我的代码了吗 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* noButton
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:yesButton];
[alertController addAction:noButton];
[self presentViewController:alertController animated:YES completion:nil];
这将给我以下结果。我想更改右侧的取消按钮
,以及左侧的确定按钮
感谢您抽出时间。将取消按钮类型的操作样式更改为
UIAlertActionStyleDefault
而不是UIAlertActionStyleCancel
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:yesButton];
[alertController addAction:noButton];
[self presentViewController:alertController animated:YES completion:nil];
现在你可以通过改变按钮的位置来改变按钮的顺序
[alertController addAction:yesButton]; //If you yesButton to appear first (left)
[alertController addAction:noButton];
UIAlertActions足够灵活,可以重新排序。它们不是固定的。人,这是一个有点旧的线程,但如果您想在右侧按钮中使用粗体字体,只需将该操作设置为警报对象的“.preferredAction”属性。希望这能有所帮助。我刚刚在swift 3/iOS10上证明了这一点,它工作得很好。它工作得很好,谢谢,但我想知道为什么两个警报操作的样式(UIAlertActionStyleDefault)是相同的?我正在尝试UIAlertActionStyleDefault和第二个UIAlertActionStyleCance l!!!样式取消将始终位于左侧!HIG并没有告诉你取消按钮应该总是在左边。这取决于用户的便利性。我同意,我还观察到一点,当iOs 9.2是正常的字体取消按钮时,iOs 8.1将显示粗体取消。但在ios 9中,UIAlertActionStyleCance将始终以粗体字母显示。您可以始终使用preferredAction方法将uialertAction加粗。只有当另一个选项是破坏性操作时,取消按钮才应位于右侧,在这种情况下,您应使用
UIAlertActionStyleDestructive
执行该操作。当右侧为UIAlertActionStyleDefault