简体   繁体   中英

Override shouldAutorotateToInterfaceOrientation defined in Category

I have the following category defined to allow orientation in a TabBarController

@implementation UITabBarController (MyApp) 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end

Now there are a couple of viewcontroller s where I don't want to allow landscape-mode. Because I used categories the methods in the viewcontroller s are ignored. Is there a way to solve this?

(I know you can subclass UITabBarController but this is discouraged by Apple themself).

You can ask the current view controller in your category like so:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [[self selectedViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

You might want to implement some more logic, depending on your needs. Also, subclassing is better in your case than a custom category, overwriting things globally might break at some point in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM