我正在尝试显示一个 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;
}
有人知道发生了什么吗?为什么按钮现在立即显示?