1

我正在使用 view.bounds 来确定按下按钮时在何处添加日期选择器视图。如果方向与导航到视图时的方向相同,则此方法效果很好。不幸的是,如果在显示视图时方向发生变化,则边界不是未注明日期的,因此选择器视图显示不正确。我需要一种方法来确定视图的实际边界。如果可能的话,我想要在 iPad 和 iPhone 上都可以使用的东西,因为这是一个通用应用程序。同样在 iPad 版本中,选择器视图位于 splitview 详细信息控制器中,因此我不能只交换高度和宽度并调整导航栏。

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
   [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

   // size up the picker view to our screen and compute the start/end frame origin for      our slide up animation
   //
   // compute the start frame

    CGRect screenRect = self.view.bounds;
    CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
    CGRect startRect;
    CGRect pickerRect;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIInterfaceOrientationIsPortrait(orientation))
    {
        // code for Portrait orientation
        startRect = CGRectMake(0.0,
                               screenRect.origin.y + screenRect.size.height,
                               pickerSize.width, pickerSize.height);
        pickerRect = CGRectMake(0.0,
                                screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                screenRect.size.width,
                                pickerSize.height);

    }
    else
    {
        //code for landscape
        startRect = CGRectMake(0.0,
                               screenRect.origin.y + screenRect.size.height,
                               pickerSize.width, pickerSize.height);
        pickerRect = CGRectMake(0.0,
                                screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                screenRect.size.width,
                                pickerSize.height);


    }

    self.pickerView.frame = startRect;

    // start the slide up animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    // we need to perform some post operations after the animation is complete
    [UIView setAnimationDelegate:self];

    self.pickerView.frame = pickerRect;
}
4

2 回答 2

0

如果我正确理解您的问题,您应该尝试设置:

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

在您的视图上,以便它们在方向更改时“更新”。

于 2013-01-15T15:42:09.720 回答
0

我最终将pickerview添加到操作表中,如下所示:

UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:@"Choose Date and Time"
                                                 delegate:self
                                        cancelButtonTitle:nil
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:nil];


[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];

pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];

[pickerDateToolbar setItems:barItems animated:YES];

[aac addSubview:pickerDateToolbar];
[aac addSubview:datePicker];

[aac showFromTabBar:self.tabBarController.tabBar];
CGFloat wideth = self.view.frame.size.width;
if (UIDeviceOrientationIsLandscape([self interfaceOrientation])) {
   [aac setBounds:CGRectMake(0,0,wideth, 355)];
   [datePicker sizeToFit];
} else {
    [aac setBounds:CGRectMake(0,0, wideth, 470)];
    [datePicker sizeToFit];
}
picker = aac;

希望这对其他人有帮助。

于 2013-03-04T16:02:44.800 回答