IPad不会改变屏幕旋转时的弹出位置

时间:2014-02-01 22:48:39

标签: ipad popover

我有一个应用程序,您可以旋转视图横向或纵向,当您执行此操作时按钮移动位置。我通过在旋转变化时更改约束来完成此操作。

问题是,当您选择此按钮时,我会显示弹出窗口。因此,用户可以选择按钮,弹出窗口,旋转屏幕,按钮移动但弹出窗口仍然指向按钮的旧位置。

我通过以下代码调用popover。

    TableViewController = [[TableViewPopover alloc] initWithNibName:@"TableViewPopover" bundle:nil];
    [TableViewController setDelegate:self];
    TableViewPopoverController = [[UIPopoverController alloc] initWithContentViewController:TableViewController];
    [TableViewPopoverController setDelegate:self];

    TableViewPopoverController.popoverContentSize = CGSizeMake(TableViewController.view.frame.size.width, TableViewController.view.frame.size.height);


    if ([TableViewPopoverController isPopoverVisible])
    {
        [TableViewPopoverController dismissPopoverAnimated:YES];
    }
    else
    {
        CGRect popRect = CGRectMake((self.LoginButtonProp.frame.origin.x),
                                    (self.LoginButtonProp.frame.origin.y),
                                    (self.LoginButtonProp.frame.size.width),
                                    (self.LoginButtonProp.frame.size.height));
        [TableViewPopoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }

是否有一个简单的解决方案来纠正这种情况。

由于

1 个答案:

答案 0 :(得分:0)

解决方案是在UIPopoverController:回调中再次显示didRotateFromInterfaceOrientation

请考虑以下代码段。

typedef void (^PopoverControllerPresentationBlock)(void);

@interface ViewController () <UIPopoverControllerDelegate>
@property (nonatomic, strong) UIPopoverController *popoverController;
@property (nonatomic, copy) PopoverControllerPresentationBlock popoverControllerPresentationBlock;
@end

@implementation ViewController

- (IBAction)showButtonPressed:(id)sender {
    __block __weak UIButton *button = sender;
    __block __weak ViewController *weakSelf = self;

    // Save the presentation action in a block
    self.popoverControllerPresentationBlock = ^(void){
        [self.popoverController presentPopoverFromRect:[button convertRect:button.bounds toView:weakSelf.view]
                                                inView:weakSelf.view
                              permittedArrowDirections:UIPopoverArrowDirectionUp
                                              animated:YES];
    };

    // invoke the presentation block to show the popover now
    self.popoverControllerPresentationBlock();
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if (self.popoverControllerPresentationBlock) {
        // While orientation changed, re-invoke the presentation block
        self.popoverControllerPresentationBlock();
    }
}

#pragma mark UIPopoverViewControllerDelegate

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    // Clear the presentation block so the popover won't be re-presented after user really dismiss it
    self.popoverControllerPresentationBlock = nil;
    self.popoverController = nil;
}

@end

希望它有所帮助。