2016-05-24 42 views
3

私はUIAlertControllerフレームの幅を取得したいので、UIAlertControllerに付属するUIPickerViewとUIToolbarの幅を指定することができます。UIAlertControllerのフレーム幅を取得するにはどうすればよいですか?

次の文を使用して幅を取得しようとしました。

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

答えて

4

身長にアクセスするには、NSLayoutConstraintを作成する必要があります。

スイフト3

let alertController = UIAlertController(title:"Title", message: "Message", preferredStyle: .alert) 


let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in 
    // hide action sheet 
} 
alertController.addAction(cancelAction) 


let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.80) 
alertController.view.addConstraint(height); 
self.present(alertController, animated: true, completion: nil) 
関連する問題