-1

I have custom ViewController and want to show it as alert. When i do it like below, my custom ViewController does not fill the entire content of the UIAlertController and scroll bar is shown. How to do that and also how to disable scrolling.

let alert : UIAlertController =  UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.setValue(myCustomViewController, forKey: "contentViewController")
self.present(alert, animated: true) 
nihasmata
  • 652
  • 1
  • 8
  • 28
  • 4
    You don't. `UIAlertController` does not support any such customization. Don't use `UIAlertController` for this. – rmaddy Jan 06 '19 at 22:17

2 Answers2

2

I don't think you can do that. You might consider presenting your custom view controller in such a way it resembles an alert controller. A tip is to play around with modalPresentationStyle of your custom view controller. With that you can make a semi-transparent backgrounded view controller that looks like an UIAlertController. For example:

myCustomViewController.modalPresentationStyle = .overCurrentContext
myCustomViewController.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
present(myCustomViewController, animated: true, completion: nil)
alanpaivaa
  • 1,959
  • 1
  • 14
  • 23
0

Yes you can.

Here's an example

var content_view_height:CGFloat = 200
var ctrl = MyViewController()
var alert = UIAlertController()

alert.setValue(ctrl, forKey: "contentViewController")
ctrl.preferredContentSize.height = content_view_height
alert.preferredContentSize.height = content_view_height

// you have to adjust the Alert size so it's bigger than your custom Content View Controller
var alert_height:CGFloat = 350
let custom_alert_height:NSLayoutConstraint = NSLayoutConstraint(
                item: alert.view,
                attribute: NSLayoutConstraint.Attribute.height,
                relatedBy: NSLayoutConstraint.Relation.equal,
                toItem: nil,
                attribute: NSLayoutConstraint.Attribute.notAnAttribute,
                multiplier: 1,
                //constant: self.view.frame.height * 0.50 // e.g. half of current view
                constant: alert_height // fixed
        )

alert.view.addConstraint(custom_alert_height);
// then present it