Barks
Barks

Reputation: 151

showInView is causing clipping

So, I followed a tutorial concerning how to get a a UIDatePicker up instead of the keyboard when the user taps on a UITextField. The problem I'm getting is that when the picker comes up, it's clipped and it gives this message in the console:

Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:].

I tried the other methods but they didn't work either, nor did I expect them to. If anyone could take a look at the code below and offer some advice, that would be great. Thanks :)

AddShiftViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Shift.h"

@interface AddShiftsViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *jobTitleField;
@property (weak, nonatomic) IBOutlet UITextField *startDateField;
@property (weak, nonatomic) IBOutlet UITextField *endDateField;

@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *endDate;

@property (nonatomic, strong) UIActionSheet *dateSheet;

- (void)setDate;
- (void)dismissPicker;
- (void)cancelPicker;

- (IBAction)addShift:(id)sender;

@end

AddShiftViewController.m

#import "AddShiftsViewController.h"

@interface AddShiftsViewController ()

@end

@implementation AddShiftsViewController
@synthesize jobTitleField;
@synthesize startDateField;
@synthesize endDateField;
@synthesize startDate, endDate, dateSheet;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    AppDelegate *dataCenter = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (void)viewDidUnload
{
    [self setJobTitleField:nil];
    [self setStartDateField:nil];
    [self setEndDateField:nil];
    [self setEndDate:nil];
    [self setStartDate:nil];
    [self setDateSheet:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)addShift:(id)sender {
    Shift *newShift = [[Shift alloc] init];
}

- (void)setDate {
    dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    [dateSheet setActionSheetStyle:UIActionSheetStyleDefault];

    CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
    UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [picker setDatePickerMode:UIDatePickerModeDateAndTime];

    [dateSheet addSubview:picker];

    UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];

    [controlToolBar setBarStyle:UIBarStyleDefault];
    [controlToolBar sizeToFit];

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPicker)];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPicker)];

    [controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];

    [dateSheet addSubview:controlToolBar];

    [dateSheet showInView:self.view];

    [dateSheet setBounds:CGRectMake(0, 0, 420, 385)];
}

- (void)dismissPicker {
    NSArray *listOfViews = [dateSheet subviews];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM d, yyyy at H:mm"];

    for (UIView *subView in listOfViews) {
        if ([subView isKindOfClass:[UIDatePicker class]]) {
            if (self.startDateField.isEditing) {
                self.startDate = [(UIDatePicker *)subView date];
                [self.startDateField setText:[formatter stringFromDate:self.startDate]];
                [dateSheet dismissWithClickedButtonIndex:0 animated:YES];
            }
            else if (self.endDateField.isEditing) {
                self.endDate = [(UIDatePicker *)subView date];
                [self.endDateField setText:[formatter stringFromDate:self.endDate]];
                [dateSheet dismissWithClickedButtonIndex:0 animated:YES];
            }
        }
    }
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [self setDate];
    return NO;
}

- (void)cancelPicker {
    [dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

@end

Shifts.h (custom class)

#import <Foundation/Foundation.h>

@interface Shift : NSObject

@property (nonatomic, strong) NSString *jobTitle;
@property (nonatomic, strong) NSString *employer;
@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *endDate;

@end

EDIT

So, I tried switching to:

[dateSheet showFromTabBar:self.tabBarController.tabBar];

But it still is clipping. I've attached a picture to show you exactly what's happening.

enter image description here

Upvotes: 0

Views: 1798

Answers (2)

Nethfel
Nethfel

Reputation: 46

It looks like you may have gotten the numbers on this line a bit off:

[dateSheet setBounds:CGRectMake(0, 0, 420, 385)];

Should be:

[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];

The width of the iPhone screen isn't 420 pixels :) the classic screen is 320 wide (in portrait) or 640 wide on the Retina display. Although there could be an error somewhere else as well, this one stands out to me.

Upvotes: 3

atastrophic
atastrophic

Reputation: 3143

First, are you sure the Frame Width n Height are Correct.

CGRect pickerFrame = CGRectMake(0, 44, 0, 0); // (params) x , y, width, height

Picker View using this frame has 0 width and 0 height, thus it won't be able to show itself properly. Try creating the UIDatePicker with some Width or Height.

Also, are you trying to Display UIActionSheet from withtin a controller that is part UITabBarController ... ??? If so, then instead of

[datesheet showInView:self.view];

use

[datesheet showFromTabBar:self.tabBarController.tabBar];

Upvotes: 0

Related Questions