[英]iOS Present View Controller Within Popup
所以我有我的主視圖控制器。 該視圖控制器有一個帶有故事板segue的條形按鈕項目,其類型設置為Present As Popover。
這一切都按預期工作。 但是當您點擊該彈出窗口控制器中的另一個按鈕時,它會全屏顯示視圖。 我希望它在popover中顯示。 就像在現有的popover界限之上推/顯示一樣。
我怎樣才能做到這一點?
我只想在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;
}
迅速:
@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)從UIBarButtonItem創建一個UIViewController(藍色)和ctrl + drag(鼠標)到UIViewController並選擇“present as Popover”(就像你做的那樣)。
2)單擊UIViewController(藍色)並單擊Editor-> embed in-> Navigation Controller(這將是讓下一個控制器保持在彈出窗口中的技巧)
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)
}
額外:
如果您不想使用popUp,您還可以將segue種類從barButtonItem更改為navigationController
目前模態
和演示到類似的東西
表單
在故事板中。
一目了然,即使您不需要navigationBar,也應始終使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.