这一切都按预期工作。但是当你在那个弹出窗口视图控制器内点击另一个按钮时,它会将视图全屏显示。我希望它能在弹出窗口中显示。像推/显示在现有弹出窗口边界的顶部。
我该如何实现这一点?
我只想在iPad上实现此行为。但我认为这是默认的。
编辑
我更喜欢在故事板中完成所有操作,但如果需要,也愿意使用Swift代码完成。
UIBarButtonItem
的 IBAction
,并在动作中执行以下操作:- (IBAction)popupAction:(UIBarButtonItem *)sender {
UIViewController *vc = [[UIViewController alloc] init]; // I don't use segue
vc.view.backgroundColor = [UIColor grayColor];
vc.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popvc = vc.popoverPresentationController;
popvc.delegate = self;
popvc.permittedArrowDirections = UIPopoverArrowDirectionAny;
popvc.barButtonItem = sender; // if UIBarButtonItem
// if view
// popvc.sourceView = sender;
// popvc.sourceRect = sender.bounds;
vc.preferredContentSize = CGSizeMake(200, 200);
[self presentViewController:vc animated:YES completion:nil];
}
还有 UIPopoverPresentationControllerDelegate
:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection{
return UIModalPresentationNone;
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return YES;
}
Swift:
@IBAction func popupAction(_ sender: UIBarButtonItem) {
let vc = UIViewController.init()
vc.view.backgroundColor = UIColor.gray
vc.modalPresentationStyle = UIModalPresentationStyle.popover
let popvc = vc.popoverPresentationController
popvc?.delegate = self
popvc?.permittedArrowDirections = UIPopoverArrowDirection.any
popvc?.barButtonItem = sender
vc.preferredContentSize = CGSize.init(width: 200, height: 200)
self.present(vc, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
return true
}
UIBarButtonItem
将视图控制器显示为弹出窗口,但是当您单击弹出窗口内部的按钮时,它不会在弹出窗口内显示,而是全屏显示。 - Charlie FishUIBarButtonItem
显示了一个弹出窗口。在这个视图控制器/弹出窗口中,我有按钮。我希望这些按钮在原始弹出窗口内显示视图控制器,而不是作为全屏幕视图控制器。我更喜欢只使用 storyboard 来实现这一点,而不是使用代码。但如果必须使用代码也可以。使用 storyboard 更加清晰简洁。 - Charlie FishUIBarButtonItem
,你需要使用 sourceView
和 sourceRect
。我还添加了一个弹出另一个视图控制器的图片。 - tomfriwel这仅适用于Storyboard。
1)创建一个UIViewController(蓝色),并从您的UIBarButtonItem上按住ctrl+拖动(鼠标),将其拖到UIViewController上,并选择“present as Popover”(就像你做的那样)。
2)单击UIViewController(蓝色),然后单击编辑器 ->嵌入 ->导航控制器(这将是让下一个控制器保留在弹出窗口中的技巧)
3)创建第二个UIViewController(绿色)
4)在第一个UIViewController(蓝色)中创建一个UIButton,然后从按钮上按住ctrl +拖动到第二个UIViewController(绿色)上,选择“show”
最终在Storyboard中应该看起来像这样:
结果如下:
如果您想在没有navigationBar的情况下使用PopOver,则可以在蓝色控制器中使用:
self.navigationController?.isNavigationBarHidden = true
如果要从绿色视图返回到蓝色视图,可以在绿色控制器中使用以下方法:
@IBAction func backToBlueController(sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
补充:
如果您不想使用弹出窗口,您还可以将segue种类从barButtonItem更改为navigationController到
模态呈现
并在Storyboard中将演示方式更改为
表单表格
一目了然的是,即使您不需要navigationBar,您也应始终使用UINavigationController来管理导航,因为导航控制器为您提供了一个导航堆栈,可以从中弹出和推入。
dismiss(animated:false,completion:nil)
。有没有办法在'blue'视图中关闭整个弹出窗口,在'green'视图中返回到'blue'视图?我知道这可能可以通过导航栏来解决,但它并不符合应用程序的风格。此外,在iPhone上,导航栏在“blue”视图上没有关闭按钮。 - Charlie Fishdismiss
来关闭弹出视图。我的最后一个问题是,是否可以在没有导航控制器和弹出视图的情况下显示 green
视图。我是否可以只做类似于 if self.navigationController?
的操作,并在其中放置 self.navigationController.popViewControllerAnimated(true)
,并在 else 语句中放置 dismiss(animated: false, completion: nil)
?如果没有导航控制器并且它是从另一部分显示的而不是从导航控制器或弹出视图中显示的,那么这样会回到 dismiss
吗? - Charlie Fish你尝试过EzPopup(https://github.com/huynguyencong/EzPopup)吗?它是一个Swift pod。只需要几行代码:
// init YourViewController
let contentVC = ...
// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)
// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)