[英]Reducing the frame size of a uiviewcontroller being presented modally with option UIModalPresentationOverCurrentContext
[英]Determining if a UIViewController is being presented modally
我的應用程序的主窗口包含一個基於xib的UITabBarController(在Interface Builder中完全配置),也可以模態呈現(很像Music.app“將歌曲添加到播放列表”模態視圖)。 UITabBarController包含許多UINavigationControllers,后者又包含子類UITableViewControllers。 這就是我當前正在檢測是否在模式UITabBarController中呈現子類UITableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.isModal = NO;
UIViewController *child = self;
UIViewController *parent = self.parentViewController;
while (parent) {
if (parent.modalViewController && parent.modalViewController == child) {
self.isModal = YES;
break;
}
child = parent;
parent = parent.parentViewController;
}
if (self.isModal) {
// modal additions, eg. Done button, navigationItem.prompt
}
else {
// normal additions, eg. Now Playing button
}
}
有沒有辦法做到這一點,不涉及走向parentViewController樹或子類化所有中間視圖控制器,以在初始化時傳遞isModal狀態?
如果您正在尋找iOS 6+,這個答案已被棄用,您應該查看Gabriele Petronella的答案
我剛才回答了一個非常類似的問題,我有一個函數來確定當前控制器是否顯示為模態,而不需要在這里子類化標簽欄控制器:
是否可以確定ViewController是否顯示為Modal?
在原始答案中,有一些關於這個功能如何工作的基本解釋,如果需要你可以在那里檢查,但在這里
-(BOOL)isModal {
BOOL isModal = ((self.parentViewController && self.parentViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
( self.navigationController && self.navigationController.parentViewController && self.navigationController.parentViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] parentViewController] isKindOfClass:[UITabBarController class]]);
//iOS 5+
if (!isModal && [self respondsToSelector:@selector(presentingViewController)]) {
isModal = ((self.presentingViewController && self.presentingViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
(self.navigationController && self.navigationController.presentingViewController && self.navigationController.presentingViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);
}
return isModal;
}
由於iOS5的你也可以在一個的viewController實例中使用isBeingPresented:
- (BOOL)isModalViewController
{
return [self isBeingPresented];
}
在Twitter上得到了答案 。 我最后UITabBarController
了UITabBarController
並添加了一個BOOL isModal
實例屬性,當以模態方式呈現時,該屬性設置為YES。 然后子視圖可以使用self.tabBarController
和子類的isModal
來訪問isModal
屬性並相應地渲染/表現。
我將看看獲取根視圖控制器並檢查它是否有模態視圖控制器。 您可以從UIWindow獲取該視圖控制器。 另請注意,您可以使用UINavigationController的viewControllers屬性遍歷當前視圖控制器的層次結構:for(UIViewController * viewController in self.navigationController.viewControllers){...}更快更簡單。
您可以在顯示視圖時在自定義初始化程序中設置顯示狀態。 我的意思是呈現它的代碼將知道它是如何呈現的,對吧?
- (void)initInModalMode:(BOOL)isModal
它比后來追溯發現其狀態更好嗎?
在這些斯威夫特時代,有一種更容易的方式。
extension UIViewController {
var isPresentedModally: Bool {
return presentingViewController?.presentedViewController == self || parent?.isPresentedModally == true
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.