2011-11-30 131 views
1

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

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

在我的应用程序中,我创建的肖像模式运行良好。但是当我尝试在横向模式下编写代码时,它可能定位错误。我怎样才能做到这一点。 如何更改横向模式。任何人都可以帮助我。提前致谢。

+0

我有横向模式的选择器的问题,并解决它与此stackoverflow后:http://stackoverflow.com/questions/1088163/loading-a-uidatepicker-in-landscape-on-a-uinavigationcontroller看看是否这有助于。 – chris

回答

0

你只需要设置大小检查属性是这样的:

enter image description here

附: 编辑1:你必须在

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

{ 
return YES; 
} 

编辑2是返回:

如果你正在做这样你就不需要,如果条件检查方向,使框架它。

+0

不要按照这个答案,否则你的(iPad)应用很可能会被拒绝。苹果公司的HIG清楚地表明,你不应该以这种方式改变UIPickerView的外观(相信我,我已经完成并被拒绝)。而应考虑在iPad上的UIPopover中呈现UIPickerView。 – Till

+0

@Till:你可能是对的..但它的内置属性在Xcode中......我不会修改PickerView ..你也许是正确的..如果它真的会被拒绝,我会删除这个答案.. :-)) –

+0

好的如何解决这个问题。 – akk