How to add a UIStepper to AlertView

1k views Asked by At

I want to add a UIStepper to my alerView but the stepper is not showing here is my code

 var alert = UIAlertView(title: "Hello works", message: "\n\n", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "")
 var stepper = UIStepper()
    stepper.frame = CGRect(x: CGFloat(12.0), y: CGFloat(5.0), width: CGFloat(100), height: CGFloat(10))
    alert.addSubview(stepper)
    alert.show()
2

There are 2 answers

0
Kostas Tsoleridis On

UIAlertView is deprecated. You should use UIAlertController instead. Here is an answer that explains how you can implement what you want using a UIAlertController:

UIAlertController - add custom views to actionsheet

0
Rizwan Shaikh On
//  Below is code for implementing UIAlertView Using UIAlertController in swift and add your custom views on it .


        let logoutAlert = UIAlertController(title: "Alert", message: "DemoAlert", preferredStyle: UIAlertControllerStyle.alert)



        logoutAlert.addAction(UIAlertAction(title: "cancel", style: .default, handler: nil))



        logoutAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in

            // Write your code here

        }))

        var stepper = UIStepper()
        stepper.frame = CGRect(x: CGFloat(12.0), y: CGFloat(5.0), width: CGFloat(100), height: CGFloat(10))

        // You can add any view on UIAlert controller using below code:

        logoutAlert.popoverPresentationController?.sourceRect = stepper.frame

        logoutAlert.popoverPresentationController?.sourceView = stepper


        self.present(logoutAlert, animated: true, completion: nil)