Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

iphone - UIPicker sizing in landscape mode

I am trying to develop an app with a UIPicker in landscape mode, taking up (almost) the entire width of the screen (with 5 or 6 components). Can you please tell me how to set the size of UIPicker. Thank you very much for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually, I resize my pickers for almost every app. I do not like that they take up the entire screen. Here is the method that I am using: (note that I am also rotating the picker to be horizontal)

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

Then, I like to add a background image for my view that covers up the grey bars (which are now on the top and bottom) of the 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 delegate method:

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

}

This gives a much smaller, horizontal picker with a nice look and feel. I hope this helps someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...