DixieFlatline
DixieFlatline

Reputation: 8035

Size of UIPickerView is not set correctly

I need to show a view that is not full screen and has a button and pickerView under it. I tried using this code:

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20,20,200,200)];
        container.backgroundColor=[UIColor whiteColor];

        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        myButton.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y+5, 170, 20); // position in the parent view and set the size of the button
        myButton.titleLabel.textColor=[UIColor redColor];
        myButton.titleLabel.text=@"click me";
        //myButton.backgroundColor=[UIColor blueColor];
        //[myButton backgroundImageForState:<#(UIControlState)#>[UIImage imageNamed:@"iPhone_mainbutton_green.png"];
        // add targets and actions
        [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        // add a buttonview
        [container addSubview:myButton];

        UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];
        //piker.numberOfComponents=1;
        piker.showsSelectionIndicator=YES;
        //piker.delegate=self;
        //piker.dataSource=self;

        [container addSubview:piker];

        [myButton release];
        [piker release];

        [self.view addSubview:container];

and i get this (picker out of the screen and very large, not 100x100):

enter image description here

Upvotes: 2

Views: 4010

Answers (3)

TheTiger
TheTiger

Reputation: 13354

You are adding your UIPickerView as subview of "container"

UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];

this means pickerview takes its origin from container not from UIView if you want it to on right position do this :-

UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
[container addSubview:picker];

just focus on your origin and remember every subview takes it origin from its parent view. and One more thing apple doesn't allow to change pickerview Height so you cant set it 100x100 you can just change of its width. UIPickerView supports only 3 height value and these are 216.0 , 180.0, 162.0 try to set height only from these 3 value. it will be work.

let me know if you have any query regarding this.

Upvotes: 2

rich.e
rich.e

Reputation: 3740

If you want to adjust the frame, do it in your view controller's viewWillAppear: method - you can adjust it there.

Make sure that you also implement widthForComponent: and the width of all your components combined is less then the frame width - picker inset. I use the following:

- (void)viewDidLoad{
    self.picker = [[[UIPickerView alloc] init] autorelease];
    [self.view addSubview:self.picker];  
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.settingsPicker.frame =  CGRectMake(0.0, 100.0, self.view.frame.size.width, 100.0);
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    CGFloat guessedPickerInsetWidth = 24;
    CGFloat pickerWidth = self.view.frame.size.width - guessedPickerInsetWidth;
    if (component == SettingsPickerFirstComponent) {
        return pickerWidth * 0.4; // make the first component 40%
    }
    return pickerWidth * 0.3; // only two others, make them 30% each
}

Upvotes: 0

Gypsa
Gypsa

Reputation: 11314

you are adding you pickeer view in container and container frame is:-(20,20,200,200)

make it(0,20,200,200).

Upvotes: 2

Related Questions