swift – iOS in UIAlertController’s customizing view, I need to know the which means of “contentViewController” in setValue(_:forKey:) one other a number of questions


I have been interested by it earlier than, however I am unable to discover a option to reply it, so I am penning this.

My present state is to create a UIAlertController and put a textField between the title and actionButton.

let alert = UIAlertController(title: nil, message: "enter person's identify : )", preferredStyle: .alert)
let btn   = UIAlertAction(title:"OK",model:.default)
    (_) in 
    // Later, when the person inputs tf, the alert is terminated and the label textual content of different views will likely be up to date.

Now, I feel two methods enter tf between alert’s title part and motion button part.

  • Within the First case, ( use UIView )
let centerView = UIView()
let tf         = UITextField()
tf.body       = CGRect(x:0,y:0,width: self.view.body.width,peak: 30)
tf.textual content        = "홍길동"



I get the outcome like this picture.

Now, right here is the primary query.

I did not specify the body of the centerView, and the subView is wrapped within the centerView. Why does the centerView comply with the CGPoint of the subView?…

Additionally, why cannot UIView have an space that’s not less than as massive as tf?…

On this query, I need to know the reply.

  • Within the Second case, ( use UIViewController )
let centerVC = UIViewController()
let tf       = UITextField()
tf.body     = CGRect(x: 0, y: 0, width: self.view.body.width, peak: 20)
tf.textual content      = "홍길동"
content material.view.addSubview(tf)

and add this code

content material.preferredContentSize = CGSize(width: tf.body.width, peak: tf.body.peak) 

Second query.

Why would not the code under work?

and eventually add this code

alert.setValue(contents, forKey: "contentViewController")

Then I can create the alert I would like.


Final query.

Why do I have to put “contentViewController” in setValue? “contentViewController” just isn’t outlined anyplace, however is it working correctly? TT..
And if UIView fails, have you learnt why I ought to select UIViewController?

Please clear up my questions.. Thanks.


Please enter your comment!
Please enter your name here