UIPicker / UIActionSheet 在 iPhone 上工作正常,但在 iPad 上只有细线

发布于 2024-11-26 21:14:46 字数 1937 浏览 3 评论 0原文

以下代码在 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)];

The following Code works fine on iPhone, but not on iPad. Only get a thin line instead of the full view. Have been researching and maybe something with the rect for the picker? All size numbers seem to be reasonable.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

方圜几里 2024-12-03 21:14:46

中来使其工作

    - (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" 

操作表的 。我猜是魔法。

Well, got it to work by putting most of the code in

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet

including

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

AND

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

for the action sheet. Magic I guess.

鸵鸟症 2024-12-03 21:14:46

这是正确的吗:

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 工作正常吗?

Is this correct:

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

Is your isipad bool working ok?

下壹個目標 2024-12-03 21:14:46

感谢您的帖子 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

}

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

Thanks for your post ort11.
I needed to make an iPad app and I did working copy-paste code from iPhone app and on iPad is the same: a small line :)

My needs is a language chooser only, nothing else.

Here are the codes based on your answer:

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

}

at runtime I do a cache in a variable:

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

and here is the finish:

- (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

}

I am not able to move this actionsheet from the center of the screen to botton- left corner. But I am trying...

请止步禁区 2024-12-03 21:14:46

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

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

I think this would be the cleanest solution :

@interface MyActionSheet : UIActionSheet

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

@end

@implementation MyActionSheet

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

@end

then:

MyActionSheet * s = [[MyActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
s.customView = _auxView;
[s addSubview:_auxView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文