Is there any way to add UIPickerView into UIAlertController (Alert or ActionSheet) in Swift?

后端 未结 11 1306
旧巷少年郎
旧巷少年郎 2020-12-01 06:16

I\'m totally new to swift (and iOS programming at all), but I started messing around with it (it wasn\'t a good idea when everything is still beta version :D). So I tried to

相关标签:
11条回答
  • 2020-12-01 06:59

    Try this I done some trick...

    Below code is working for me in iPod iOS8beta5 + XCode6
    I add UIActivityIndicatorView control in UIAlertController in objective-c.

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                            message:@"Please wait\n\n\n"
                                     preferredStyle:UIAlertControllerStyleAlert];
    
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        spinner.center = CGPointMake(130.5, 65.5);
        spinner.color = [UIColor blackColor];
        [spinner startAnimating];
        [alert.view addSubview:spinner];
        [self presentViewController:alert animated:NO completion:nil];
    

    enter image description here


    Note :
    It's in objective-c but by this it's prove that we can also do this using swift.

    0 讨论(0)
  • 2020-12-01 07:00

    I think it is not bad idea to start from beta. You need custom UIAlertView. Pay attention to UIAlertController it's available only in iOS 8.0.

    Look there and there

    0 讨论(0)
  • 2020-12-01 07:00

    You can add Picker like UIActionSheet in iOS 8 using Objective-C by:-

     colorArray = [[NSMutableArray alloc ]initWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", nil];
    picker = [[UIPickerView alloc]init];
    picker.frame = CGRectMake(0.0, 44.0,self.view.frame.size.width, 216.0);
    picker.dataSource = self;
    picker.delegate = self;
    picker.showsSelectionIndicator = true;
    picker.backgroundColor = [UIColor whiteColor];
    
    UIToolbar* pickerDateToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];  //(frame: CGRectMake(0, 0, 320, 44))
    pickerDateToolbar.barStyle =  UIBarStyleBlackTranslucent;//UIBarStyle.Black
    pickerDateToolbar.barTintColor = [UIColor whiteColor];
    pickerDateToolbar.translucent = true;
    actionView.backgroundColor = [UIColor whiteColor];
    
    
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(150, 5, 150, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(0, 1);
    label.font = [UIFont systemFontOfSize:15];//[UIFont boldSystemFontOfSize:15];
    label.text = @"Select a Status";
    UIBarButtonItem *labeltext= [[UIBarButtonItem alloc] initWithCustomView:label];
    
    
    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel_clicked:)];
    
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(done_clicked:)];
    
    pickerDateToolbar.items = [[NSArray alloc] initWithObjects:cancelBtn,flexSpace,labeltext,doneBtn,nil];
    
    cancelBtn.tintColor = [UIColor blueColor];
    doneBtn.tintColor = [UIColor blueColor];
    
    [actionView addSubview:pickerDateToolbar];
    [actionView addSubview:picker];
    
    if (window != nil) {
    [window addSubview:actionView];
    }
    else
    {
    [self.view addSubview:actionView];
    }
    
    [UIView animateWithDuration:(0.2) animations:^{
    actionView.frame = CGRectMake(0, self.view.frame.size.height - 260.0, self.view.frame.size.width, 260.0);
    self.view.alpha = 0.5;
    actionView.alpha = 1;
    }];
    

    In ViewDidLoad add this,

    UIApplication * delegate = [UIApplication sharedApplication];
    UIWindow *myWindow = delegate.keyWindow;
    NSArray *myWindow2 = delegate.windows;
    
    if(myWindow == [UIApplication sharedApplication].keyWindow)
    window = myWindow;
    else
    window = myWindow2[0];
    
    actionView = [[UIView alloc]initWithFrame:CGRectMake(0, UIScreen.mainScreen.bounds.size.height, UIScreen.mainScreen.bounds.size.width, 240.0)];
    

    // This is to Create ActionView and for faded background when picker is displayed in popup.

    -(void)cancel_clicked:(id)sender{
    NSLog(@"cancel clicked");
    [UIView animateWithDuration:0.2 animations:^{
    actionView.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height - 260.0, UIScreen.mainScreen.bounds.size.width, 260.0);
    } completion:^(BOOL finished) {
    self.view.alpha = 1;
    [actionView removeFromSuperview];
    }];
    

    }

    // Cancel Button Click Method.

    -(void)done_clicked:(id)sender{
    NSLog(@"done clicked");
    self.view.userInteractionEnabled = TRUE;
    
    [UIView animateWithDuration:0.2 animations:^{
    actionView.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height - 260.0, UIScreen.mainScreen.bounds.size.width, 260.0);
    } completion:^(BOOL finished) {
    _permitStatus.text = [colorArray objectAtIndex:[picker selectedRowInComponent:0]];
    self.view.alpha = 1;
    [actionView removeFromSuperview];
    }];
    

    }

    // For Done Button Click.

    0 讨论(0)
  • 2020-12-01 07:03

    Swift 5.2 / Xcode 11.6 / iOS 11.4

    func addAlert(){
    
        // create the alert
        let title = "This is the title"
        let message = "This is the message"
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert);
        alert.isModalInPopover = true;
    
        // add an action button
        let nextAction: UIAlertAction = UIAlertAction(title: "Action", style: .default){action->Void in
            // do something
        }
        alert.addAction(nextAction)
    
        // now create our custom view - we are using a container view which can contain other views
        let containerViewWidth = 250
        let containerViewHeight = 120
        let containerFrame = CGRect(x:10, y: 70, width: CGFloat(containerViewWidth), height: CGFloat(containerViewHeight));
        let containerView: UIView = UIView(frame: containerFrame);
    
        alert.view.addSubview(containerView)
    
        // now add some constraints to make sure that the alert resizes itself
        let cons:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1.00, constant: 130)
    
        alert.view.addConstraint(cons)
    
        let cons2:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.greaterThanOrEqual, toItem: containerView, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1.00, constant: 20)
    
        alert.view.addConstraint(cons2)
    
        // present with our view controller
        present(alert, animated: true, completion: nil)
    
    }
    
    0 讨论(0)
  • 2020-12-01 07:04

    I needed to do the same thing and this is how I resolved it. I did something similar to Jageen. My code is below. Note that I put values in the UIPickerView in a different part of the code.

    //Need to use an UIAlertController for iOS 8 instead of an action view
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                  message:@"\n\n\n\n\n\n\n\n"
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    //Make a frame for the picker & then create the picker
    CGRect pickerFrame = CGRectMake(0, 0, 270, 100);
    UIPickerView *regionsPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
    
    //There will be 3 pickers on this view so I am going to use the tag as a way
    //to identify them in the delegate and datasource
    regionsPicker.tag = 1;
    
    //set the pickers datasource and delegate
    regionsPicker.dataSource = self;
    regionsPicker.delegate = self;
    
    //set the pickers selection indicator to true so that the user will now which one that they are chosing
    [regionsPicker setShowsSelectionIndicator:YES];
    
    //Add the picker to the alert controller
    [alert.view addSubview:regionsPicker];
    
    //make the toolbar view
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
    toolView.backgroundColor = [UIColor blackColor]; //set it's background
    
    
    //add buttons to the view
    CGRect buttonFrame = CGRectMake(0, 5, 100, 30); //size & position of the button as placed on the toolView
    //make the cancel button & set the title
    UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
    [button setTitle: @"Cancel" forState: UIControlStateNormal];
    [button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal]; //make the color blue to keep the same look as prev version
    [toolView addSubview:button]; //add to the subview
    
    //Add the tartget
    [button addTarget: self
               action: @selector(cancelRegionSet)
     forControlEvents: UIControlEventTouchDown];
    
    //do the same for the select button
    buttonFrame = CGRectMake(90, 5, 100, 30);
    UIButton *selButton = [[UIButton alloc] initWithFrame:buttonFrame];
    [selButton setTitle:@"Select" forState:UIControlStateNormal];
    [selButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [toolView addSubview:selButton];
    
    [selButton addTarget: self
                  action: @selector(dismissRegionSet:)
     forControlEvents: UIControlEventTouchDown];
    
    //add the toolbar to the alert controller
    [alert.view addSubview:toolView];
    
    [self presentViewController:alert animated:NO completion:nil];
    
    0 讨论(0)
  • 2020-12-01 07:08

    Here is a github project that pops up a UIDatePicker in the center of the screen. It does not use UIAlertController or UIAlertView. I don't think that's the intent of the alert classes.

    0 讨论(0)
提交回复
热议问题