9

我正在尝试在横向模式下开发一个带有 UIPicker 的应用程序,占用(几乎)屏幕的整个宽度(带有 5 个或 6 个组件)。你能告诉我如何设置 UIPicker 的大小吗?非常感谢您的帮助。

4

7 回答 7

9

实际上,我几乎为每个应用程序调整了我的选择器的大小。我不喜欢它们占据整个屏幕。这是我正在使用的方法:(请注意,我还将选择器旋转为水平)

在 viewDidLoad .....

picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = NO;


//Resize the picker, rotate it so that it is horizontal and set its position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57);  
rotate = CGAffineTransformScale(rotate, .46, 2.25);
CGAffineTransform t0 = CGAffineTransformMakeTranslation(3, 22.5);
picker.transform = CGAffineTransformConcat(rotate,t0);
[self.view addSubview:picker];
[picker release];

然后,我想为我的视图添加一个背景图像,覆盖 UIPicker 的灰色条(现在位于顶部和底部):

//Create the overlay to superimpose ontop of the picker
//In this case, the image is the size of the screen with a transparent area the size of the //UIPickerView cut out in the middle

UIImageView *uiiv = [[UIImageView alloc] 
                     initWithImage:[UIImage                                 imageNamed:@"problem_bg.png"]];
uiiv.userInteractionEnabled = NO;
uiiv.opaque = YES; //for performance
[self.view addSubview:uiiv];
[uiiv release];

UIPickerView 委托方法:

-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)rowforComponent:(NSInteger)component reusingView:(UIView *)view
 {

    UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 280)] autorelease];
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage.png"]];

    img.frame = CGRectMake(0, 0, 102,280);
    img.opaque = YES;
    [viewForRow addSubview:img];
    [img release];
    UILabel *label;

    UIFont *font = [ UIFont fontWithName:@"Helvetica"  size:20];

    label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 270, 100)] autorelease];

    label.text = @"I am a label";
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.font = font;
    label.backgroundColor = [UIColor clearColor];
    label.opaque = NO;
        [viewForRow addSubview:label];

    CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57);  
        [viewForRow setTransform:rotate]; 
    return viewForRow;

}

这提供了一个更小、具有漂亮外观和感觉的水平选择器。我希望这可以帮助别人。

于 2009-09-14T06:03:43.570 回答
4

您可以通过在 TextEdit 中打开 .xib 文件并更改 UIPickerView 的大小来编辑大小。

于 2009-09-08T18:40:39.103 回答
4

你不能。UIPickerView 的大小是恒定的。

更新:事实证明你可以调整 UIPickerView 的大小。诀窍是将它放在另一个(较小的)UIView 中,并调整该视图的大小。我还没试过这个。

更新 2:此方法不会调整 UIPickerView 的大小,而是裁剪它。它可能是也可能不是您正在寻找的东西,但是 AFAIK,没有办法真正调整 UIPickerView 的大小,这已经很接近了。它看起来没那么糟糕。

更新 3(姗姗来迟):从 SDK 3.0 开始,UIPickerView 可以使用 initWithFrame 或 setFrame 完全调整大小。

于 2009-02-11T02:20:46.983 回答
1

我与调整选取器视图大小的相同问题作斗争。经过一些研究和头痛之后,您可以轻松做到:

  1. 忘记界面生成器!我认为如果没有界面构建器,您可以更好地使用 UI。
  2. 在您的控制器中覆盖加载视图方法。

    -(void)loadView
    {
            //create your view
     self.view = [[UIView alloc] initWithFrame:CGRectZero];
     // create a default sized pickerView
     pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
     pickerView.delegate = self;
     pickerView.dataSource = self;
     pickerView.showsSelectionIndicator = YES;
    
      // Get its frame
     CGRect pickerFrame = pickerView.frame;
    
       // Set it to what ever you like, I use a default screen height times a shrink factor like 0.75 for 3/4 of its original size
     pickerFrame.size.width = screenWidth*pickerShrinkFactor;
     pickerFrame.size.height =  pickerHeight*pickerShrinkFactor;
     // You can also set the upper left corner of the picker
     pickerFrame.origin.x = 0;
     // Set the picker frame to new size and origin
            pickerView.frame = pickerFrame;
            // Add it to your view
     [self.view addSubview:pickerView];
    
    }
    

祝你好运

于 2010-06-30T22:53:46.733 回答
0

The answer is in:

-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

Since with:

- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component

Makes it work (that is: sideways with scaled label) of course

于 2010-01-02T19:11:01.647 回答
0

我相当确定 UIPicker 有一种尺寸,您无法更改。有兴趣听到不同的声音。

于 2009-02-11T02:22:11.317 回答
0

没问题(我刚注册)。顺便说一句,我刚刚意识到,当我发布我的答案时,该方法(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent .... 丢失了 row 和 forComponent 之间的空间。因此,请确保您的委托正确,否则它将无法正常工作。

于 2009-12-23T06:56:31.803 回答