简体   繁体   中英

How to lock changing orientation on device?

How to lock changing orientation on device?

I tried to use

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return UIInterfaceOrientationLandscapeRight;
}

and this

- (void)viewDidAppear:(BOOL)animated
{
    [self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
    [super viewDidAppear:animated];
}

but nothing work when i use device. When app runs on simulator it works great.

simulator have iOS 6 and device 5.1.1. What i'm doing wrong?

Try this:

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

The method returns YES or NO answering the question "should I rotate to this orientation: interfaceOrientation". So you should return YES or NO - UIInterfaceOrientationLandscapeRight is not a BOOL. But you can use a comparison to return the BOOL like described above.

Open your project, select project file, goto summary section, and set desired interface orientation on 'Supported Interface Orientations' subsection. Good Luck!(for iOS 6.x)

In your project .plist file add Supported interface orientations

and set the item to Landscape(right home button)

If you want to do it only for 1 viewcontroller, you can do that either by

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

or by

//deprecated
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

and

- (BOOL)shouldAutorotate {
return self.interfaceOrientation == UIInterfaceOrientationPortrait;
}

If you want to set it for the whole app, make sure you change your Info.plist file and add Supported Interface Orientations

You just add following lines in .m file

#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

Then you have to add following methods in .m file

#ifdef IOS_OLDER_THAN_6

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#endif

#ifdef IOS_NEWER_OR_EQUAL_TO_6

- (NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscapeRight;
}
#endif

For iOS 6 support include these two methods

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

Also kindly note that this will work on xCode 4.5 +

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