iOS 在弹出窗口中显示视图控制器
iOS Present View Controller Within Popup
所以我有我的主视图控制器。该视图控制器有一个条形按钮项,带有一个故事板 segue,类型设置为 Present As Popover。
这一切都按预期工作。但是,当您点击该弹出视图控制器中的另一个按钮时,它会显示全屏视图。我希望它显示在弹出窗口中。就像 push/display 在现有的弹出框边界之上。
我怎样才能做到这一点?
我只希望在 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
}
这适用于 iPad 和 iPhone。
结果:
在弹出控制器中弹出另一个 viewController:
这仅适用于 Storyboard。
1) 创建一个 UIViewController(蓝色)并按住 ctrl 键并拖动(鼠标)从您的 UIBarButtonItem 到 UIViewController 和 select "present as Popover"(就像您所做的那样)。
2) 点击 UIViewController(蓝色),点击 Editor->embed in->Navigation Controller(这将是让下一个控制器留在弹出窗口中的技巧)
3) 创建第二个 UIViewController(绿色)
4) 在第一个 UIViewController(蓝色)中创建一个 UIButton,然后 ctrl + 从按钮拖动到第二个 UIViewController(绿色)和 select "show"
最后在 Storyboard 中应该是这样的:
结果:
如果你想要没有导航栏的 PopOver,你可以在蓝色控制器中使用:
self.navigationController?.isNavigationBarHidden = true
并从绿色返回到蓝色视图,您可以在绿色控制器中使用:
@IBAction func backToBlueController(sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
补充:
如果您不想使用弹出窗口,您还可以将 segue 类型从 barButtonItem 更改为 navigationController 到
Present Modally
以及类似
的演示文稿
Form Sheet
在情节提要中。
乍一看,您应该始终使用 UINavigationController 来管理您的导航,即使您不需要导航栏,因为导航控制器为您提供了一个导航堆栈,您可以从中弹出和推入它。
你试过了吗 - 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)
所以我有我的主视图控制器。该视图控制器有一个条形按钮项,带有一个故事板 segue,类型设置为 Present As Popover。
这一切都按预期工作。但是,当您点击该弹出视图控制器中的另一个按钮时,它会显示全屏视图。我希望它显示在弹出窗口中。就像 push/display 在现有的弹出框边界之上。
我怎样才能做到这一点?
我只希望在 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
}
这适用于 iPad 和 iPhone。
结果:
在弹出控制器中弹出另一个 viewController:
这仅适用于 Storyboard。
1) 创建一个 UIViewController(蓝色)并按住 ctrl 键并拖动(鼠标)从您的 UIBarButtonItem 到 UIViewController 和 select "present as Popover"(就像您所做的那样)。
2) 点击 UIViewController(蓝色),点击 Editor->embed in->Navigation Controller(这将是让下一个控制器留在弹出窗口中的技巧)
3) 创建第二个 UIViewController(绿色)
4) 在第一个 UIViewController(蓝色)中创建一个 UIButton,然后 ctrl + 从按钮拖动到第二个 UIViewController(绿色)和 select "show"
最后在 Storyboard 中应该是这样的:
结果:
如果你想要没有导航栏的 PopOver,你可以在蓝色控制器中使用:
self.navigationController?.isNavigationBarHidden = true
并从绿色返回到蓝色视图,您可以在绿色控制器中使用:
@IBAction func backToBlueController(sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
补充:
如果您不想使用弹出窗口,您还可以将 segue 类型从 barButtonItem 更改为 navigationController 到
Present Modally
以及类似
的演示文稿Form Sheet
在情节提要中。
乍一看,您应该始终使用 UINavigationController 来管理您的导航,即使您不需要导航栏,因为导航控制器为您提供了一个导航堆栈,您可以从中弹出和推入它。
你试过了吗 - 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)