How do I get the frame width of UIAlertController?
Asked Answered
T

1

6

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];
Transalpine answered 24/5, 2016 at 9:41 Comment(0)
A
1

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

Atomy answered 30/9, 2016 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.