Change Orientation in iOS 9 - ios

My app will be launched in Landscape,only one view can change to Portrait, in which there is a button,I click this button to change the orientation to Portrait.
It performed as expected on iPad(iOS 8.4,Deployment target: 7.0),and I made an iPhone version,in which I just made some adjust on UI,and it runs on iPhone 6 Plus simulator(Base SDK: 9.1,Deployment target: 7.0),but the button for chagning orientation doesn't work.
And I want to know how to implement this in iOS 9;
The Below is my code:
- (void)changeOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation==UIInterfaceOrientationLandscapeLeft||orientation==UIInterfaceOrientationLandscapeRight){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationLandscapeLeft) {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
}
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScreenToPortrait" object:nil];
}else{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
if (orientation==UIInterfaceOrientationPortrait) {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
}else {
value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
}
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScreenToLandscapeLeft" object:nil];
}
[UIViewController attemptRotationToDeviceOrientation];
}

This works on iOS9
NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue: orientationValue forKey:#"orientation"];
Go to General tab from app target, and enable all orientations.
For all view which support only landscape orientation implement supportedInterfaceOrientations() and return landscape orientation value.

seen or set tick on General setting.

Related

How to change the orientation of the screen on button click?

I have tried many codes from this website and others too.... But nothing seems to be working for me. Here is my code...
- (IBAction)orientation:(id)sender {
self.currentDeviceOrientation = [[UIDevice currentDevice] orientation];
int cur = self.currentDeviceOrientation;
int port = UIInterfaceOrientationPortrait;
int landl = UIInterfaceOrientationLandscapeLeft;
int landr = UIInterfaceOrientationLandscapeRight;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
NSNumber *value= [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"orientation"];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
}
}
on the click of a button, I want the screen to change from its current orientation i.e, if the screen is in portrait mode, it should change in to landscape and vice-verse.
Try this and change as what you need.
- (IBAction)orientation:(id)sender {
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
NSLog(#"landscape left");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
NSLog(#"landscape right");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
NSLog(#"portrait");
}
}
You need to set orientation as per your needs.
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];

Change orientation fails on iOS 9.2

This line of code always fail on iOS 9.2 Simulator, but it is ok on iOS 8.4 and iOS 10.1 Simulators
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:#"orientation"];
It trows this error
XPC connection interrupted
Terminating since there is no system app.
I created a new MasterDetail Application in XCode and this is viewDidLoad method in the DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:#"orientation"];
}];
}

change orientation when video is finished in iOS

I think my coding on done button is not working as i am changing my screen orientation when video is starting and when video is finished i need to come back to my normal orientation my coding is.
vid = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//[self presentModalViewController:vid animated:YES];
[self presentMoviePlayerViewControllerAnimated:vid];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(donebutton) name:MPMoviePlayerDidExitFullscreenNotification object:vid];
and then the coding done button method
-(void) donebutton{
NSLog(#"done");
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
Hope this code will help you to change device orientation when a video playing is completed.
-(void)playVideo {
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void)moviePlayBackDidFinish:(NSNotification *)notification {
moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// If the moviePlayer.view was added to full screen, it needs to be removed
moviePlayer.fullscreen = NO;
}

iBeacon to push another view! iOS App

What I am trying to do is make it so that when an iBeacon has been found (when the user is on the home screen), it sends them to another part of the app. Essentially it pushes another view of the app from the storyboard. For example, iBeacon1 pushes View1, iBeacon2 pushes View2, etc, and that's it. Like I said I know that it possible, but because iBeacons are quite new, there isn't much help available.
I would really appreciate it if you could take some time out of your day to help me with this issue that I cannot find the answer to.
Thank you for your time,
Josh
If you are using Storyboards, you could do something like this in your AppDelegate:
var launchedViewControllers = [Int]()
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let navController = self.window?.rootViewController?.navigationController {
for beacon in beacons {
if (beacon.minor == 1) {
if !launchedViewControllers.contains(beacon.minor.integerValue) {
launchedViewControllers.append(beacon.minor.integerValue)
let viewController1 = mainStoryboard.instantiateViewControllerWithIdentifier("viewController1")
navController.pushViewController(viewController1, animated: true)
}
}
if (beacon.minor == 2) {
if !launchedViewControllers.contains(beacon.minor.integerValue) {
launchedViewControllers.append(beacon.minor.integerValue)
let viewController2 = mainStoryboard.instantiateViewControllerWithIdentifier("viewController1")
navController.pushViewController(viewController2, animated: true)
}
}
}
}
}
You could also rewrite this to use segues instead of programmatically pushing ViewControllers using a NavigationController but both will work.
pragma mark -
pragma mark -Important Methods
-(void)showSecondController{
if ([_beacon.major isEqual: #3404] && [_beacon.minor isEqual: #10692]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SDOVideoDetailController"];
} else if ([_beacon.major isEqual: #20165] && [_beacon.minor isEqual: #53849]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LRCVideoDetailController"];
} else if ([_beacon.major isEqual: #53088] && [_beacon.minor isEqual: #30487]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CECSVideoDetailController"];
} else if ([_beacon.major isEqual: #55304] && [_beacon.minor isEqual: #59835]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IVideoDetailController"];
} else if ([_beacon.major isEqual: #16004] && [_beacon.minor isEqual: #46054]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CILVideoDetailController"];
} else if ([_beacon.major isEqual: #20214] && [_beacon.minor isEqual: #11696]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ITVideoDetailController"];
} else if ([_beacon.major isEqual: #21559] && [_beacon.minor isEqual: #17557]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PARKVideoDetailController"];
} else if ([_beacon.major isEqual: #59452] && [_beacon.minor isEqual: #21013]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SCVideoDetailController"];
} else if ([_beacon.major isEqual: #42093] && [_beacon.minor isEqual: #49773]) {
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:#"orientation"];
self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"BAVideoDetailController"];
}

how to set a viewController only landscape in iOS8.0

for i want to build a movie controller,and it`s pushed by a navigationController,how can i set it ,also only this vc can be landscape,others should only be portraitUp,please help,thanks in advance.
Try..
- (void)viewDidAppear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}
- (void)viewDidDisappear:(BOOL)animated
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
}

Resources