如何在风景中更改uipickerview?

时间:2011-11-30 07:56:15

标签: iphone uipickerview

我在这里开发应用程序。我无法在横向模式下更改pickerview。这是我的代码。

  -(IBAction) showPicker:(UIControl *)sender
   {
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@""
                                                  delegate:self
                                         cancelButtonTitle:@"Done"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];
menu.tag=1;

UIPickerView *pickerView;
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
    self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{      

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-80,70,220,0)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;    

    [menu addSubview:pickerView];

    [menu showInView:self.view.superview];

    [menu setBounds:CGRectMake(0,0,320, 470)];

    [pickerView release]; 
    [menu release]; 

    NSLog(@"Landscape");
}
else 
{
    NSLog(@"Portrait");
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,70,220,0)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;    

    [menu addSubview:pickerView];

    [menu showInView:self.view.superview];

    [menu setBounds:CGRectMake(0,0,320, 470)];

    [pickerView release]; 
    [menu release]; 

}
 }


 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
return [arrayNo count];

}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
[entered2 resignFirstResponder];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
entered2.text=[arrayNo objectAtIndex:row];
[entered2 resignFirstResponder];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
return [arrayNo objectAtIndex:row];
}
 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component     
{
int sectionWidth = 300;
return sectionWidth;
}

在我的应用程序中我创建了肖像模式运行正常。但是当我尝试在横向模式下编写代码时,它可能会错误地定位。我怎样才能做到这一点。 我该如何改变横向模式。有人帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您只需要设置尺寸检查器属性,如下所示:

enter image description here

P.S。 修改1 :您必须在

中返回yes
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
return YES;
 }

编辑2:

如果你这样做,你不需要条件来检查方向并为它做框架。