简体   繁体   中英

How can I build custom UIAlertController in Xamarin.iOS?

I am currently dealing with the design which cannot be build using UIAlertController. How can I customize it?

You shouldn't change the AlertView, the recommended way is to use it as it, but if you really want to create your own Alert modal, you should use a ViewController. The Apple Docs state:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

So, you basically have to create a new Alert that inherits from UIViewController that's displayed as a popup, as you can see here

First you need clearly explain which effect you want in question.This document Displaying Alerts in Xamarin.iOS maybe helpful .

okayCancelButton.TouchUpInside += ((sender, e) => {

    //Create Alert
    var okCancelAlertController = UIAlertController.Create("Alert Title", "Choose from two buttons", UIAlertControllerStyle.Alert);

    //Add Actions
    okCancelAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine ("Okay was clicked")));
    okCancelAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, alert => Console.WriteLine ("Cancel was clicked")));

    //Present Alert
    PresentViewController(okCancelAlertController, true, null);
});

Unfortunately there is not much customization available for UIAlertController. You have to use Telerik UI (TKAlert View). And then on that TKAlert View you can add your custom view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM