如何获得UIAlertController的帧宽? [英] How do I get the frame width of UIAlertController?
问题描述
我想获得UIAlertController框架的宽度,以便我可以指定UIPlertController附带的UIPickerView和UIToolbar的宽度。
我是尝试使用以下语句来获取宽度。
alertPicker.view.frame.size.width
但是,宽度与viewController的相同:
self.view .frame.size.width
想知道UIAlertController的宽度吗?非常感谢。
这是我的代码。
UIAlertController * alertPicker = [UIAlertController alertControllerWithTitle:nil message:@\ n \ nn \ n \\ nn \\ n \\ n \ n \ n \\ n \\ n \\ n \\ n \\ n \\ npreferredStyle: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:@Donestyle: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];
UIAlertController
继承自 UIViewController
,因此您可以获得的
并获取宽度。 UIView
的大小UIAlertController
UIAlertController的可见宽度是UIWindow的宽度(纵向模式)减去10个填充点无论设备方向如何,每一方都是
( Swift 4 )
let alertController = UIAlertController(标题:标题,消息:消息, preferredStyle:.alert)
print(alertController.view.frame.size.width - 20.0)
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];
UIAlertController
inherits from UIViewController
, so you can get the size of the UIView
of the UIAlertController
and get the width.
The visible width of the UIAlertController is the width of the UIWindow (in portrait mode) minus 10 points of padding on each side, irregardless of the device orientation
(Swift 4)
let alertController = UIAlertController(title:"Title", message: "Message", preferredStyle: .alert)
print(alertController.view.frame.size.width - 20.0)
这篇关于如何获得UIAlertController的帧宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!