Is it possible for my UIViewController to detect if it is displayed in the UIPopoverController or not?

I would like to use one view controller for both iPhone and iPad PopOverView. If the view is displayed in a popover, I would like to do a little reformatting of the user interface.

Is it possible for my UIViewController to detect if it is displayed in the UIPopoverController?

I found the contentSizeForViewInPopover property, which is great for resizing a view, but I would like to remove / hide the element if the view is loaded in PopOverView.

+7
source share
5 answers

I do not think that's possible. I tried to take a look at the parentViewController class as well as the presentingViewController class, and both are null. If they do not provide a UIViewController property similar to the navigationController property, this is not possible.

+1
source

you can override the method below in your class and control it using the BOOL variable or some functions

- (CGSize)contentSizeForViewInPopover { popovermode = YES; [self callhideMethod]; return CGSizeMake(320, 200); } 

it can help you.

+1
source

My approach was to use / create different subclasses of the UIViewController for each of the two types of presentations. Quite often, they can share a common superclass. Here is an example:

 @interface CMDetailsViewController : UIViewController @end @interface CMDetailsSinglePageViewController : CMDetailsViewController @end @interface CMDetailsPopoverViewController : CMDetailsViewController @end 

Each of these two classes can customize some of the types of behavior defined in your base class. In your case, it will be presentation logic, which, I believe, is in one of the appearance methods (for example, -(void)viewWillAppear:(BOOL)animated or alternative) or -(void)viewDidLoad .

As you definitely know how you represent the view controller: using let UINavigationController (on the iPhone) or UIPopoverController (on the iPad), you can decide which of these two subclasses to create.

All in all, this is my default approach when I work on a generic iOS application. The system allows you to define 2 different UIApplicationDelegates for each platform, which means that you can use the corresponding UIViewControllers without having a ton of if-else to check the device on which the application was run.

0
source

You can try this. I have not tested this.

  if ([viewcontroller.parentViewController isKindOfClass:[UIPopoverController class]]) { //do something... } 

I think that might work.

-one
source

You can easily check if your device is an iPad or iPhone, and make adjustments.

Use something like this

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // The device is an iPad } else { // The device is an iPhone or iPod touch. } 
-one
source

All Articles