Add UIPickerView & a Button in Action sheet - How?

后端 未结 11 1642
遥遥无期
遥遥无期 2020-11-22 15:40

My application requires following things to be added in an action sheet.

  • UIToolbar
  • Button on UIToolbar
  • UIPicker Control

I have

11条回答
  •  礼貌的吻别
    2020-11-22 16:23

    I don't really understand why the UIPickerView is going inside a UIActionSheet. This seems to be a messy and hacky solution, which can be broken in a future iOS release. (I've had things like this break in an app before, where the UIPickerView wasn't being presented on the first tap and had to be retapped - weird quirks with the UIActionSheet).

    What I did is simply implement a UIPickerView and then added it as a subview to my view, and animate it moving up as though it were being presented like an action sheet.

    /// Add the PickerView as a private variable
    @interface EMYourClassName ()
    
    @property (nonatomic, strong) UIPickerView *picker;
    @property (nonatomic, strong) UIButton *backgroundTapButton;
    
    @end
    
    ///
    /// This is your action which will present the picker view
    ///
    - (IBAction)showPickerView:(id)sender {
    
        // Uses the default UIPickerView frame.
        self.picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
    
        // Place the Pickerview off the bottom of the screen, in the middle set the datasource delegate and indicator
        _picker.center = CGPointMake([[UIScreen mainScreen] bounds].size.width / 2.0, [[UIScreen mainScreen] bounds].size.height + _picker.frame.size.height);
        _picker.dataSource = self;
        _picker.delegate = self;
        _picker.showsSelectionIndicator = YES;
    
        // Create the toolbar and place it at -44, so it rests "above" the pickerview.
        // Borrowed from @Spark, thanks!
        UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
        pickerDateToolbar.barStyle = UIBarStyleBlackTranslucent;
        [pickerDateToolbar sizeToFit];
    
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
    
        // The action can whatever you want, but it should dimiss the picker.
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(backgroundTapped:)];
        [barItems addObject:doneBtn];
    
        [pickerDateToolbar setItems:barItems animated:YES];
        [_picker addSubview:pickerDateToolbar];
    
        // If you have a UITabBarController, you should add the picker as a subview of it
        // so it appears to go over the tabbar, not under it. Otherwise you can add it to 
        // self.view
        [self.tabBarController.view addSubview:_picker];
    
        // Animate it moving up
        [UIView animateWithDuration:.3 animations:^{
            [_picker setCenter:CGPointMake(160, [[UIScreen mainScreen] bounds].size.height - 148)]; //148 seems to put it in place just right.
        } completion:^(BOOL finished) {
            // When done, place an invisible button on the view behind the picker, so if the
            // user "taps to dismiss" the picker, it will go away. Good user experience!
            self.backgroundTapButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _backgroundTapButton.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            [_backgroundTapButton addTarget:self action:@selector(backgroundTapped:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:_backgroundTapButton];
        }];
    
    }
    
    // And lastly, the method to hide the picker.  You should handle the picker changing
    // in a method with UIControlEventValueChanged on the pickerview.
    - (void)backgroundTapped:(id)sender {
    
        [UIView animateWithDuration:.3 animations:^{
            _picker.center = CGPointMake(160, [[UIScreen mainScreen] bounds].size.height + _picker.frame.size.height);
        } completion:^(BOOL finished) {
            [_picker removeFromSuperview];
            self.picker = nil;
            [self.backgroundTapButton removeFromSuperview];
            self.backgroundTapButton = nil;
        }];
    }
    

提交回复
热议问题