如何更改 uipickerview 横向?

发布于 2024-12-18 20:40:17 字数 2270 浏览 0 评论 0原文

我在这里开发应用程序。我无法在横向模式下更改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;
}

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

Here i developing application. I could not change pickerview in landscape mode. Here is my code.

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

In my application i created portrait mode ran fine. But when i try to write code in landscape mode it can positioned wrongly. How can i do this.
How can i change landscape mode. Anybody help me. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2024-12-25 20:40:17

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

在此处输入图像描述

PS 编辑 1:您必须在

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
return YES;
 }

编辑2:

中返回yes:如果您这样做,则不需要检查方向的条件并为其制作框架。

You just need to set size inspector attributes like this:

enter image description here

P.S. Edit 1:You have to return yes in

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{
return YES;
 }

Edit 2:

If you are doing like this you don't need if condition for checking orientation and make frame for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文