Search code examples
ioswidthuipickerviewuitoolbaruialertcontroller

How do I get the frame width of UIAlertController?


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];

Solution

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