How do I get the frame width of UIAlertController?

对着背影说爱祢 提交于 2019-11-30 17:11:26

问题


I'd like to get the width of UIAlertController frame so that I can specify the width of UIPickerView and UIToolbar which will come with the UIAlertController.

I've tried to use the following statement to get the width.

alertPicker.view.frame.size.width

But, the width is as same as viewController's:

self.view.frame.size.width

Any idea to get the width of the UIAlertController? Thanks a lot.

This is my code.

    UIAlertController *alertPicker = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

    CGRect pickerFrame = CGRectMake(8, 0, alertPicker.view.frame.size.width, self.view.frame.size.height/2);
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;

    UIToolbar* pickerToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(2, 0, alertPicker.view.frame.size.width, 44)];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneBtnisClicked:)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    pickerToolbar.items = [[NSArray alloc] initWithObjects:flexSpace,doneBtn,nil];

   [alertPicker.view addSubview:pickerToolbar];
   [alertPicker.view addSubview:pickerView];

回答1:


There is no direct way to get the UIAlertController width. How to set height and width of a UIAlertController in IOS 8



来源:https://stackoverflow.com/questions/37409826/how-do-i-get-the-frame-width-of-uialertcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!