UIPicker / UIActionSheet在iPhone上运行良好但在iPad上只有细线

时间:2011-07-28 20:55:33

标签: ipad uiactionsheet uipicker

以下代码适用于iPhone,但不适用于iPad。只获得一条细线而不是全视图。一直在研究,也许是选择器的矩形?所有大小的数字似乎都是合理的。

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil
    cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
    if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216);
    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [actionSheet addSubview:pickerView];
    [pickerView release];
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray         arrayWithObject:@"Select"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(10, 7.0f, 100.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet:)  forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];
    [closeButton release];
    UISegmentedControl *skipButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Skip Birth Year"]];
    skipButton.momentary = YES; 
    skipButton.frame = CGRectMake(200, 7.0f, 100.0f, 30.0f);
    skipButton.segmentedControlStyle = UISegmentedControlStyleBar;
    skipButton.tintColor = [UIColor blackColor];
    [skipButton addTarget:self action:@selector(skipActionSheet:)   forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:skipButton];
    [skipButton release];
    [actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
    [actionSheet showInView:self.view];
    [actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];

4 个答案:

答案 0 :(得分:4)

嗯,通过将大部分代码放在

中来实现它
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet

包括

    pickerFrame = CGRectMake(0, 0, 320, 214);

AND

    initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 

表示操作表。魔术我想。

答案 1 :(得分:2)

这是否正确:

CGRect pickerFrame = CGRectMake(0, 40, 0, 0); //This line sets the frame height and width equal to 0.
if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216); 

你的isipad bool工作正常吗?

答案 2 :(得分:0)

感谢您的帖子ort11。 我需要制作一个iPad应用程序,我从iPhone应用程序和iPad上工作的复制粘贴代码是相同的:一小行:)

我的需求只是语言选择器,没有别的。

以下是基于您答案的代码:

- (IBAction)btLanguageChooserPressed {
    NSLog(@"btLanguageChooserPressed");


    [self createLanguageSheet];

    // show the sheet: will be invoked the willPresentActionSheet method
    [sheet showInView:self.view];
}

- (void)createLanguageSheet {
    NSLog(@"createLanguageSheet()");

    // hoolly crap, need to initialize the title with: @"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" to get working!!!
    sheet  = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 
                                         delegate:self 
                                cancelButtonTitle:nil
                           destructiveButtonTitle:nil
                                otherButtonTitles:nil];

    [sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];    

}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    CGRect pickerFrame =  CGRectMake(0, 0, 320, 216);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;

    [sheet addSubview:pickerView];


    // add close button:       
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray         arrayWithObject:@"OK"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(120, 12, 100, 30);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissSheet:)  forControlEvents:UIControlEventValueChanged];
    [sheet addSubview:closeButton];

    [sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];



    //[sheet showInView:self.view];//already called, why need again?
    //[sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];

}

在运行时我在变量中执行缓存:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"Selected: %@", [arrayLanguages objectAtIndex:row])  ;
    selectedRow = row;
}

这是结束:

- (void)dismissSheet:(id)sender {
    [sheet dismissWithClickedButtonIndex:0 animated:YES];    
    NSString* selectedLanguage = [arrayLanguages objectAtIndex:selectedRow];
    NSLog(@"dismissSheet() selectedLanguage: %@",selectedLanguage);


    switch (selectedRow) {
        case 0://English
            imgCurrentLanguage.image = [UIImage imageNamed:@"english.png"];
            break;
        case 1:// Deutsch

            break;
        case 2://Français

            break;
        case 3://Italiano

            break;
        case 4://Español

            break;

        default:
            break;
    }
    // TODO: do the changes in GUI

}

我无法将此操作表从屏幕中心移动到左下角。但我正在努力......

答案 3 :(得分:0)

我认为这将是最干净的解决方案:

@interface MyActionSheet : UIActionSheet

@property (nonatomic, strong) IBOutlet UIView * customView;

@end

@implementation MyActionSheet

-(CGRect)bounds {
    return self.customView.bounds;
}

@end

然后:

MyActionSheet * s = [[MyActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
s.customView = _auxView;
[s addSubview:_auxView];