如何获取UIAlertController的框架宽度?

Haz*_*zel 5 width uipickerview uitoolbar ios uialertcontroller

我想获取UIAlertController框架的宽度,以便可以指定UIAlertController随附的UIPickerView和UIToolbar的宽度。

我尝试使用以下语句获取宽度。

alertPicker.view.frame.size.width

但是,宽度与viewController的宽度相同:

self.view.frame.size.width

有什么想法可以获取UIAlertController的宽度吗?非常感谢。

这是我的代码。

    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];
Run Code Online (Sandbox Code Playgroud)

Eze*_*tos 1

没有直接的方法来获取宽度UIAlertController如何在 IOS 8 中设置 UIAlertController 的高度和宽度