2012-09-24 37 views
1

我做了UIPickerView并添加到viewcontroller'view。UIPickerView的组件宽度是否可以手动调整?

我手动调整了pickerview的宽度。

当你看图片,组件的内容之间不匹配的宽度以及组件的宽度和

pickerview的宽度和组件的宽度。

有没有像UILabel的sizeToFit方法?

expected code is : 
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
     switch(component) { 
      [pickerView.label sizeToFit]; 
     } 
    } 


================= Following is mycode ==================================================== 
- (void)viewDidLoad { 

    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; 

    [super viewDidLoad]; 

    UIPickerView *pickerView = [[[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)] autorelease]; 
    pickerView.dataSource = self; 
    pickerView.delegate = self; 
    pickerView.showsSelectionIndicator = YES; 

    [self.view addSubview:pickerView]; 


    activities = [[NSArray alloc] initWithObjects:@"sleeping", @"eating", @"working", @"studying", @"walking", @"thinking", @"considering", @"testing", @"talking", nil]; 
    feelings = [[NSArray alloc] initWithObjects:@"awesome", @"sad", @"happy", nil]; 

} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    switch(component) { 
     case 0: return 100; 
     case 1: return 200; 
    } 
} 

enter image description here

回答

1

尽我solution..i从来没有设置各个部件的宽度&高度... UIPickerview保持自身...所以我不担心这个... .try这只是评论整个方法称为“widthForComponent”,并确保您已经使用所有这些Picker方法propely ..“numberOfComponentsInPickerView”,“numberOfRowsInComponent”和“titleForRow”然后再次运行该项目...让我知道它会解决你的问题与否

好吧现在看到下面的方法是需要与uipickervie w ...你想要一些代码然后在这里它是

- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    // you're code for when user select any item from picker 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 2; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if(component == 0) 
        return [activities count]; 
    else if(component == 1) 
        return [feelings count]; 
    
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if(component == 0) 
        return [activities objectAtIndex:row]; 
    else if(component == 1) 
        return [feelings objectAtIndex:row]; 
    else 
     return @""; 
} 
+0

会给一些代码? –

+0

组件和组件的内容大小是否适当调整? –

+0

您是否尝试过我在上面...?,评论整个方法widthForComponent&运行项目 –

相关问题