Ios – UIalertcontroller sequence of buttons

iosuialertcontroller

I would like to change the sequence of alert action button, as per code I tried all possibility but it is not allowed me to change the sequence.

As per HIG, cancel is on right hand side
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Alerts.html#//apple_ref/doc/uid/TP40006556-CH14-SW1

See my code here

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];

this will give me following result. I want to change Cancel button on right hand and OK button on left hand side.

enter image description here

I appreciate for your time.

Best Answer

People, it's a little old thread but if you want to have the bold font in the right button, you just need to set that action as '.preferredAction' property for the alert object. Hope this can help. I've just proved it on swift 3 / iOS10 and it works just fine.

Related Topic