UIPopoverPresentationController在iOS 11

时间:2017-12-03 16:54:49

标签: ios objective-c ipad uipopoverpresentationcontroller

我试图展示一个UIPopoverPresentationController,其中有一个UIButton的导航栏。这曾经工作,但UIButton不再显示,因为iOS 11(iPad)。有趣的是,在我的弹出窗口中,我还可以推送另一个UIViewController,当我从它回来时,会出现UIButton。 这是显示弹出窗口的代码:

- (IBAction)buttonPressed:(id)sender {
    PopupViewController *popupController = [self.storyboard instantiateViewControllerWithIdentifier:@"PopupController"];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
    navController.modalPresentationStyle = UIModalPresentationPopover;
    [self presentViewController:navController animated:YES completion:nil];

    UIPopoverPresentationController *popController = [popupController popoverPresentationController];

    CGRect rect = self.button.frame;

    CGSize size = CGSizeMake(500, 400);
    popController.sourceView = self.view;
    popController.sourceRect = rect;
    popupController.preferredContentSize = size;
} 

这是弹出窗口中的代码,用于显示UIButton:

- (void)viewWillAppear:(BOOL)animated
{
    UIButton *rightButton = [[UIButton alloc]init];
    [rightButton setTitle: @"Press me" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightButton.frame = CGRectMake(0, 0, 120, 24);

    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:flexible, rightButtonItem, nil];
    self.navigationController.preferredContentSize = self.preferredContentSize;
}

有人知道发生了什么吗?为什么按钮现在立即显示?

1 个答案:

答案 0 :(得分:0)

我会将你的代码移出viewWillAppear并进入viewDidLoad。它在viewWillAppear中是你在打开另一个控制器然后返回到这个按钮时可以看到按钮的原因,但是你并不希望每次回到这个视图时都重新创建按钮。我认为这很有可能解决它最初没有出现的问题。