Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

ios - Custom View in UIAlertController

I am trying to put a custom view inside a UIAlertController. I'm running into some odd issues with the sizing of the custom view.

I want the custom view to span the width of the UIAlertController, whatever that might be. I'm using CGRectGetWidth(alertController.view.bounds) to get the width of the alert controller. However, this seems instead to be returning the width of the entire view. using view.frame does not make a difference.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"My Title" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(alertController.view.bounds), 50)];
view.backgroundColor = [UIColor blueColor];

[alertController.view addSubview:view];
[self presentViewController:alertController animated:YES completion:nil];

This yields the results below. I have the same issue trying to get X, Y width and height properties of the UIAlertController. Does anyone know how I can position this view in the middle of the alert controller without using hard-coded numbers?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're not supposed to do that. To quote the docs:

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.

If you go against an explicit statement like that from Apple all bets are off, and even if you can get it to work on the current OS version, it could break with any future version.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...