我正在尝试使用横向模式的UIPicker开发应用程序,占用(几乎)整个屏幕宽度(使用5或6个组件)。你能告诉我如何设置UIPicker的大小。非常感谢您的帮助。
答案 0 :(得分: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;
}
这提供了一个更小,更水平的选择器,具有漂亮的外观和感觉。我希望这有助于某人。
答案 1 :(得分:4)
你做不到。 UIPickerView的大小是不变的。
更新:事实证明你可以调整UIPickerView的大小。诀窍是将它放在另一个(较小的)UIView中,并调整该视图的大小。我还没试过这个。
更新2:此方法不会调整UIPickerView的大小,而是裁剪它。它可能会或可能不是您正在寻找的东西,但是AFAIK,没有办法真正调整UIPickerView的大小,而且它就像它得到的那样接近。看起来 不好。
更新3(姗姗来迟):从SDK 3.0开始,UIPickerView可以使用initWithFrame或setFrame完全调整大小。
答案 2 :(得分:4)
您可以通过在TextEdit中打开.xib文件并更改UIPickerView的大小来编辑大小。
答案 3 :(得分:1)
我在调整挑选视图大小的问题上也遇到了同样的问题。经过一些研究和头痛之后你可以轻松做到:
在您的控制器中覆盖加载视图方法。
-(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];
}
答案 4 :(得分:0)
我很确定UIPicker有一种尺寸,你无法改变。会有兴趣听到不同的。
答案 5 :(得分:0)
没问题(我刚刚注册)。顺便说一句,我刚刚意识到当我发布我的答案时,方法(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent ....
丢失了行和forComponent之间的空间。因此,请确保您的委托是正确的,否则它将无法正常工作。
答案 6 :(得分:0)
答案在:
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
以下:
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
当然
使其有效(即:横向使用缩放标签)