IOS: ориентация устройства при нагрузке
Кажется, что когда мое приложение загружается, оно не знает его текущей ориентации:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"portrait");// only works after a rotation, not on loading app
}
Как только я поворачиваю устройство, я получаю правильную ориентацию, но когда я загружаю приложение, не изменяя ориентацию, кажется, что использование [[UIDevice currentDevice] orientation]
не знает текущей ориентации.
Есть ли другой способ проверить это при первом загрузке моего приложения?
Ответы
Ответ 1
EDIT: Я неправильно прочитал ваш вопрос. Это позволит вам запустить приложение в определенных направлениях. Просто понял, что вы пытаетесь выяснить ориентацию при запуске.
Существует метод проверки ориентации строки состояния на UIApplication
:
[[UIApplication sharedApplication] statusBarOrientation];
Оригинальный ответ
Попробуйте настроить принятые приложениями ориентации устройств в файле plist:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
Это будет означать, что ваше приложение поддерживает функцию "Портрет" (кнопка "Дом" внизу), правый пейзаж и правый пейзаж.
Затем в вашем UIViewControllers вам необходимо переопределить метод shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
, чтобы вернуть YES, когда приложение должно вращаться:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
Это приведет к тому, что UIViewController будет автоматически вращаться, если устройство находится в одной из поддерживаемых ориентаций. Если вы хотите также поддерживать перевернутую ориентацию (портрет с домашней кнопкой сверху), добавьте это к вашему plist и просто верните YES из этого метода.
Сообщите нам, как это работает.
Ответ 2
Я думаю, что это сработает:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
Согласно ссылке UIDevice:
Цитата:
"Значение этого свойства всегда возвращает 0, если уведомления об ориентации не были включены вызовом beginGeneratingDeviceOrientationNotifications"
Я изначально предполагал, что это свойство всегда содержало текущую ориентацию, но не так, по-видимому. Я полагаю, что включение уведомлений обрабатывается нами за кулисами в других ситуациях, когда обычно используется свойство ориентации, поэтому не было очевидно, что это нужно делать вручную внутри делегата приложения.
Ответ 3
Еще одна вещь, которую никто не затронул, заключается в том, что вы сохраняете типы UIDeviceOrientation
в переменной UIInterfaceOrientation
. Они разные и не должны рассматриваться как равные. Обратите внимание, что UIDeviceOrientationLeft
равно UIInterfaceOrientationRight
(так как интерфейс вращается против другого по сравнению с устройством).
Ответ 4
Вы можете сделать это, вставив следующее уведомление внутри
-(void)viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
затем введите следующий метод внутри вашего класса
-(void)checkRotation:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your textField animation here
}
}
Вышеуказанный метод будет проверять ориентацию строки состояния ipad или iPhone и в соответствии с этим вы делаете анимацию в требуемой ориентации.
Ответ 5
При загрузке ориентация устройства может быть .Unknown
или .FaceUp
. Чтобы выяснить, является ли это портретным или альбомным, я использую statusBarOrientation
в качестве резервной копии, например:
var portraitOrientation = UIDevice.currentDevice().orientation == .Portrait
if UIDevice.currentDevice().orientation == .Unknown || UIDevice.currentDevice().orientation == .FaceUp {
portraitOrientation = UIApplication.sharedApplication().statusBarOrientation == .Portrait
}
Таким образом, я могу заверить, что portraitOrientation
всегда говорит мне, что устройство находится в портретном режиме, и если оно не будет в ландшафте. Даже при загрузке приложения в первый раз.
Ответ 6
проблема заключается в том, что [UIDevice currentDevice]orientation]
иногда неправильно сообщает расположение устройства.
вместо этого используйте [[UIApplication sharedApplication]statusBarOrientation]
, который является UIInterfaceOrientation
, поэтому для его проверки вам нужно использовать UIInterfaceOrientationIsLandscape(orientation)
надеюсь, что это поможет.
Ответ 7
Чтобы получить ориентацию из строки состояния, важно также, чтобы все ориентации были включены в файл plist.
Ответ 8
Swift 3 на основе кода @Marjin.
var portraitOrientation = UIDevice.current.orientation == .portrait
if UIDevice.current.orientation == .unknown || UIDevice.current.orientation == .faceUp {
portraitOrientation = UIApplication.shared.statusBarOrientation == .portrait
}
if(portraitOrientation)
{
// Portrait
}
else
{
}
Ответ 9
Попробуйте акселерометр, чтобы получить его чтение, UIAccelerometer, получить sharedAccelerometer, установить его делегат, получить показания, выяснить оттуда ориентацию.
Ответ 10
Пробовал все и никаких хороших результатов. Итак, что я сделал, поскольку я на ipad, должен был оставить всю работу методам splitViewController, чтобы аннулировать barButton:
Для портрета:
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc { NSlog(@"portrait");}
Для пейзажа:
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ NSlog(@"landscape");}
это всегда работает при загрузке.
Ответ 11
Я все еще использую этот фрагмент рабочего кода для iphone 4:
-(void)deviceOrientationDidChange:(NSNotification *)notification{
//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
int value = 0;
if(orientation == UIDeviceOrientationPortrait)
{
value = 0;
}else if(orientation == UIDeviceOrientationLandscapeLeft)
{
value = 90;
}else if(orientation == UIDeviceOrientationLandscapeRight)
{
value = -90;
}
CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(value));
[photoImageView setTransform:cgCTM];
}
Ответ 12
Это реальный ответ. Когда приложение запускается, ориентация неизвестна. Он использует mustAutorotateToInterfaceOrientation и поддерживаетсяInterfaceOrientations, чтобы решить, какую ориентацию выбрать.
Следите за тем, как я запускаю пример приложения в симуляторе iPhone 5.0 и поворачиваю его, используя приведенный ниже код и "Поддерживаемые ориентации интерфейса" со всеми 4 возможными ориентациями:
20:44:08.218 RotationTestApp Supported orientation: Portrait
20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left)
20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
Я видел множество фрагментов кода, но ни один из них не работает в целом достаточно (iPad и iPhone, iOS 5.0 +).
Вместо того, чтобы возиться с помощью try-this-try-this, поместите в корневой каталог vc:
#define ToNSString_BEGIN(T) \
NSString* T##ToNSString(T valueParameter) { \
switch (valueParameter) {
#define ToNSString_VALUE(value) \
case value: return @#value
#define ToNSString_END(T) \
} \
return @"(unknown)"; \
}
// NSString* UIInterfaceOrientationToNSString(UIInterfaceOrientation);
ToNSString_BEGIN(UIInterfaceOrientation);
ToNSString_VALUE(UIInterfaceOrientationPortrait); // 1
ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown); // 2
ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft); // 3
ToNSString_VALUE(UIInterfaceOrientationLandscapeRight); // 4
ToNSString_END (UIInterfaceOrientation);
// NSString* UIDeviceOrientationToNSString(UIDeviceOrientation);
ToNSString_BEGIN(UIDeviceOrientation);
ToNSString_VALUE(UIDeviceOrientationUnknown); // 0
ToNSString_VALUE(UIDeviceOrientationPortrait); // 1
ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown); // 2
ToNSString_VALUE(UIDeviceOrientationLandscapeLeft); // 3
ToNSString_VALUE(UIDeviceOrientationLandscapeRight); // 4
ToNSString_VALUE(UIDeviceOrientationFaceUp); // 5
ToNSString_VALUE(UIDeviceOrientationFaceDown); // 6
ToNSString_END (UIDeviceOrientation);
// Change this custom method to alter auto-rotation behavior on all supported iOS versions and platforms.
- (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation
{
NSUInteger interfaceOrientationAsMask = (1<<interfaceOrientation);
return interfaceOrientationAsMask & [self supportedInterfaceOrientations];
}
// Reads from the project's-Info.plist
- (NSUInteger)supportedInterfaceOrientations
{
static NSUInteger orientationsResult;
if (!orientationsResult) {
NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
for (id orientationString in supportedOrientations) {
if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) {
orientationsResult |= UIInterfaceOrientationMaskPortrait;
NSLog(@"Supported orientation: Portrait");
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
orientationsResult |= UIInterfaceOrientationMaskPortraitUpsideDown;
NSLog(@"Supported orientation: Portrait (upside-down)");
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
orientationsResult |= UIInterfaceOrientationMaskLandscapeRight;
NSLog(@"Supported orientation: Landscape (home button on the left)");
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {
orientationsResult |= UIInterfaceOrientationMaskLandscapeLeft;
NSLog(@"Supported orientation: Landscape (home button on the right)");
} else {
NSLog(@"Unrecognized orientation '%@' in mainBundle plist, key UISupportedInterfaceOrientations", orientationString);
}
}
}
return orientationsResult;
}
// iOS 6+ (not yet used in 6.0.1)
- (BOOL)shouldAutorotate
{
UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
BOOL result = [self allowAutoRotate:interfaceOrientationFromDevice];
NSString *currentDeviceOrientation = UIDeviceOrientationToNSString(interfaceOrientationFromDevice);
NSLog(@"shouldAutorotate: %s (current orientation %@)", result ? "YES" : "NO", currentDeviceOrientation);
return result;
}
// iOS 2.0 - 5.1 (iOS 6+ deprecated, 6.0.1 still works)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSString* orientationString;
UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
if ((int)interfaceOrientation != (int)interfaceOrientationFromDevice) {
orientationString = [NSString stringWithFormat:@"current device orientation: %@, interface orientation wants: %@",
UIDeviceOrientationToNSString(interfaceOrientationFromDevice),
UIInterfaceOrientationToNSString(interfaceOrientation)
];
} else {
orientationString = [NSString stringWithFormat:@"device orientation: %@", UIDeviceOrientationToNSString(interfaceOrientationFromDevice)
];
}
BOOL result = [self allowAutoRotate:interfaceOrientation];
NSLog(@"shouldAutorotateToInterfaceOrientation: %s (%@)",
result ? "YES" : "NO",
orientationString);
return result;
}
Проблема анимации segue по-прежнему не связана с использованием текущей ориентации. Мое предположение заключается в том, что подкласс каждого VC и определение ориентации на push/notify делегат на pop будет способом.
Также важно:
shouldAutorotateToInterfaceOrientation не работает
tabBarController и navigationControllers в ландшафтном режиме, эпизод II
Ответ 13
Попробуйте это. это работает для меня. gnarly в то время didfinishedlaunch метод не определяет ориентацию устройства. его взять по умолчанию в качестве портрета. так. Я использую для проверки ориентации штрихов. Я проверяю этот код. поместите его в метод didfinishedlaunch в appdeleget.
UIInterface Ориентация ориентации = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0) {//Default orientation
//UI is in Default (Portrait) -- this is really a just a failsafe.
NSLog("for portrait");
}else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog("portrait");
}else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
NSLog("Landscap");
}
Ответ 14
Попробуйте этот
[[UIApplication sharedApplication] statusBarOrientation];
или реализовать это в делетете приложения
(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
}
работает
Ответ 15
Все вышеподписавшиеся очень достоверные ответы: но как ОБНОВЛЕНИЕ: Apple возьмет: вы должны использовать UIStatusBar для чтения текущей ориентации устройства:
Одним из способов проверки текущей ориентации устройства является использование значений int как таковых внутри метода viewDidLoad
:
int orientationType = [[UIDevice currentDevice] orientation];
где рассмотрим следующее.,
- 1 = портрет (вправо вверх)
- 2 = портрет вверх дном
- 3 = пейзаж (справа)
- 4 = пейзаж (слева)
а затем вы можете использовать оператор IF
для вызова метода после обнаружения ориентации, так далее и т.д.:
Надеюсь, что это было немного полезно для кого-то.