在另一个视图控制器上显示一个视图控制器作为子视图(popoverController)

show one view controller on other view controller as a subview(popoverController)

你好,我想在主视图控制器上展示一个具有弹出功能的控制器,谁能帮我解决这个问题?? 我已经尝试过下面的代码,但它在所有视图中都是常见的,我希望它在视图中心以小尺寸显示。 这是下面的代码

Login *popoverController = [[Login alloc] init];
popoverController.popoverPresentationController.sourceView = self.view; //The view containing the anchor rectangle for the popover.
popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 40, 40); //The rectangle in the specified view in which to anchor the popover.
[self presentViewController:popoverController animated:YES completion:nil];

您可以使用容器视图。将容器视图添加到您的父视图,并在您的容器视图中添加其他视图控制器。

 self.addChildViewController(vc)
vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
self.container.addSubview(vc.view)
vc.didMoveToParentViewController(self)
let popOverView = self.popoverController.popoverPresentationController
            popOverView!.delegate = self
            if popOverView!.sourceView == nil
            {
                popOverView!.sourceView = self.view
                self.popoverController.superView = self

            }
            popOverView!.sourceRect = CGRectMake(“X-Define”, ““Y-Define”, “Width”, “Height”)
            popOverView!.passthroughViews = [self.view] 

            self.presentViewController(self.popoverController, animated: true, completion: nil)

试试这个,希望对你有帮助...

在 .m 文件中

假设您的故事板 segue 标识符名称是 showPopover

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSString *identifier = segue.identifier;
    if ([identifier isEqualToString:@"showPopover"]) {

        UIViewController *dvc = segue.destinationViewController;
        dvc.preferredContentSize = CGSizeMake(180,130);


        UIPopoverPresentationController *ppc = dvc.popoverPresentationController;
        if (ppc) {
            ppc.delegate = self;
        }
    }
}

同时添加:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {

    return UIModalPresentationNone;
}