9 iphone cocoa-touch landscape
我正在尝试使用横向模式的UIPicker开发一个应用程序,占用(几乎)屏幕的整个宽度(使用5或6个组件).你能告诉我如何设置UIPicker的大小.非常感谢您的帮助.
小智 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];
Run Code Online (Sandbox Code Playgroud)
然后,我想为我的视图添加一个背景图像,它覆盖了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];
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
这提供了一个更小,水平的选择器,具有漂亮的外观和感觉.我希望这可以帮助别人.
你不能。UIPickerView 的大小是恒定的。
更新:事实证明您可以调整 UIPickerView 的大小。诀窍是将其放置在另一个(较小的)UIView 中,并调整该视图的大小。我还没试过这个。
更新 2:此方法不会调整 UIPickerView 的大小,而是裁剪它。它可能是也可能不是您正在寻找的东西,但是据我所知,没有办法真正调整 UIPickerView 的大小,而这已经是最接近的了。看起来没那么糟糕。
更新 3(早就该了):从 SDK 3.0 开始,UIPickerView 可以使用 initWithFrame 或 setFrame 完全调整大小。
| 归档时间: |
|
| 查看次数: |
17340 次 |
| 最近记录: |