我正在尝试使用横向模式的UIPicker开发一个应用程序,占用(几乎)屏幕的整个宽度(使用5或6个组件).你能告诉我如何设置UIPicker的大小.非常感谢您的帮助.
实际上,我几乎每个应用程序都调整了我的选择器.我不喜欢他们占据了整个屏幕.这是我正在使用的方法:(请注意,我也将选择器旋转为水平)
在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; }
这提供了一个更小,水平的选择器,具有漂亮的外观和感觉.我希望这可以帮助别人.