2013-02-26 2 views
0

В моем приложении iphone отмечены поддерживаемые ориентации, как показано ниже.Интерфейс iPhone Ориентация

enter image description here

Но я хочу, чтобы предотвратить некоторые взгляд на автоматическое вращение, для этого я использую следующий код (iOS6)

РЕДАКТИРОВАНИЕ

-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation 
{ 
return (toInterfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (BOOL)shouldAutorotate 
{ 
return NO; 
} 

но вид все еще вращается , Пожалуйста, помогите мне решить проблему?

+0

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } – iPatel

+0

Какая версия iOS? 'ShouldAutorotateToInterfaceOrientation:' не используется в iOS 6.0+. Для этого вам нужен метод 'supportedInterfaceOrientations'. Если вы поддерживаете 5.x и 6.x, то реализуйте оба метода. – rmaddy

+0

http://stackoverflow.com/a/12995064/1339473 та же проблема, что и вы .. – QuokMoon

ответ

4

iOS 6.0 вводит новые методы вращения и не вызывает shouldAutorotateToInterfaceOrientation:.

Вы должны реализовать эти методы, чтобы поддерживать как предварительно IOS 6.0 и IOS 6.0+:

// Do as many checks as you want to allow for other orientations here for pre-iOS 6 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait); 
} 

// You can return YES here as this still checks supportedInterfaceOrientations 
// before rotating, or you can return NO to 'lock' the view in whatever it's in... 
// Making sure to return the appropriate value within supportedInterfaceOrientations 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

// return supported orientations, bitwised OR-ed together 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

Edit- Если UIViewController находится в пределах иерархии UINavigationController «s

Если Ваше мнение контроллер находится в иерархии представлений UINavigationController, методы поворота на самом деле вызываются контроллером навигации (а не контроллером) ... на мой взгляд это было реализовано смешно Apple, но вот что вы можете сделать, чтобы позволить контроллеру вида в реагировать на эти методы instead-

Создать категорию на UINavigationController с методами следующим образом:

// UINavigationController+Additions.h file 

@interface UINavigationController (Additions) 
@end 




// UINavigationController+Additions.m file 

#import "UINavigationController+Additions.h" 

@implementation UINavigationController (Additions) 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    UIViewController *viewController = [self topViewController]; 

    if ([viewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) 
    { 
     return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
    } 

    return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    UIViewController *viewController = [self topViewController]; 

    if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)]) 
    { 
     return [viewController supportedInterfaceOrientations]; 
    } 

    return [super supportedInterfaceOrientations]; 
} 

@end 

EDIT 2 - Если UIViewController находится в закладках UITabBarController «s

Там же самое если ваш контроллер просмотра находится на вкладках UITabBarController.

еще раз, создать категорию на UITabBarController, чтобы контроллер зрения реагировать вместо:

// UITabBarController+Additions.h file 

@interface UITabBarController (Additions) 
@end 

// UITabBarController+Additions.m file 

#import "UITabBarController+Additions.h" 

@implementation UITabBarController (Additions) 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    UIViewController *selectedViewController = [self selectedViewController]; 

    if ([selectedViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) 
    { 
    return [selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
    } 

    return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    UIViewController *selectedViewController = [self selectedViewController]; 

    if ([selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) 
    { 
     return [selectedViewController supportedInterfaceOrientations]; 
    } 

    return [super supportedInterfaceOrientations]; 
} 

@end 

EDIT 3

Вот также несколько ссылок на Objective-C Категории:

http://macdevelopertips.com/objective-c/objective-c-categories.html

http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

+0

Этот код поворачивает мое представление ...:( – 2013-02-26 05:18:59

+0

Какая ориентация этого взгляда начинается в ... чем-то другом, чем в портрете? –

+0

Если вы просто хотите ** заблокировать ** вид на все, что у него есть, имейте 'shouldAutorotate' return NO. –

0

shouldAutorotateToInterfaceOrientation осуждаются в прошивке 6.

Проверьте ответы this question для получения более подробной информации.

0

Если вы хотите, чтобы зафиксировать ориентацию определенной точки зрения попробовать это,

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; //IOS_6 
} 

EDIT

Как у Вас есть панель вкладок вы должны иметь работу вокруг этого. Посмотрите на ссылку. Надеюсь, что это поможет.

  1. Link 1
  2. Link 2
+0

И мне нужно выбрать все« поддерживаемые Interface Orientations "too..right ?? – 2013-02-26 05:22:03

+0

У вас есть контроллер навигации для этого вида? –

+0

Иерархия контроллера табло .. – 2013-02-26 05:38:43

0

Насколько я понимаю, Вы бы лучше также написать antoher два метода следующим образом

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
0

Вы действительно хотите ориентировку Upside Down или просто Портрет? я построил мое приложение для портретной ориентации, попробуйте этот код (для портретной ориентации, если другие модификации)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
if(interfaceOrientation == UIInterfaceOrientationPortrait) 
    return YES; 

return NO; 
} 
+0

Не будет работать в iOS 6.0+ .. вам необходимо реализовать новые методы вращения, основанные на его 'поддерживаемых ориентациях интерфейса '. –

0

Когда UINavigationController или UITabBarController участвует, подкласс UINavigationController/UITabBarController и overriding supportedInterfaceOrientations.

#import "UINavigationController+Orientation.h" 

@implementation UINavigationController (Orientation) 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return [self.topViewController supportedInterfaceOrientations]; 
} 

-(BOOL)shouldAutorotate 
{ 
    return [self.topViewController shouldAutorotate]; 
} 

@end 

// Для UITabBarController

#import "UITabBarController+Orientation.h" 

@implementation UITabBarController (Orientation) 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // You do not need this method if you are not supporting earlier iOS Versions 
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return [self.selectedViewController supportedInterfaceOrientations]; 
} 

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

@end 

Теперь, IOS контейнеры (например, UINavigationController) не консультироваться со своими детьми, чтобы определить, должны ли они авторотацией.
Как подклассы
1. Добавьте новый файл (Objective c- категории под прикосновением какао)
2. Category: Ориентация Category On: UINavigationController
3. Добавьте приведенный выше код UINavigationController+Orientation.m

+0

Хорошо .. Я добавил класс для UINavigationController И что мне делать в контроллерах – 2013-02-27 10:20:48

Смежные вопросы