0

我正在尝试显示一个 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;
}

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

4

1 回答 1

0

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

于 2017-12-03T17:27:12.123 回答