开发者

Is it possible for my UIViewController to detect whether it is being displayed in a UIPopoverController or not?

开发者 https://www.devze.com 2023-02-07 20:45 出处:网络
I would like to use one vi开发者_JS百科ew controller for both an iPhone view and an iPad PopOverView. If the view is displayed in a popover, I would like to do some minor reformatting of the UI.

I would like to use one vi开发者_JS百科ew controller for both an iPhone view and an iPad PopOverView. If the view is displayed in a popover, I would like to do some minor reformatting of the UI.

Is it possible for my UIViewController to detect whether it is being displayed in a UIPopoverController?

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


I do not think it is possible. I've tried looking at the class of the parentViewController, as well as the class of the presentingViewController, and both are null. Without them providing a property on UIViewController similar to the navigationController property, it can't be done.


you can override below method in your class and manage it with BOOL variable or some functions

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

it might help you.


My approach would be to use/create different UIViewController subclasses for each of the 2 kinds of presentation. 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 2 classes can customise some of the behaviours defined in your base class. In your case it will be a presentation logic, which I guess is located in one of the appearance methods (like -(void)viewWillAppear:(BOOL)animated or alternative) or -(void)viewDidLoad.

As you definitely know the way you're presenting the view controller: using let's say a UINavigationController (on the iPhone) or UIPopoverController (on the iPad), you can decide which one of these 2 subclasses to create.

In general that would be my default approach also when I work on a universal iOS application. System lets you to define 2 different UIApplicationDelegates for each platform, which means you can use appropriate UIViewControllers without having tons of if-else for checking the device the app was launched on.


You could easily check if your device is an iPad or an iPhone and make your 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.
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消