Use notifications to control the screen switching.
Or the entire app is a vertical screen of most of the interface, an interface can be switched between the picture and screen.
First, the "General" and "Device Orientation" settings only support portrait screen, like this:
Device Orientation
Then register the notification in the Viewdidload in the Special View controller:
[[Uidevice Currentdevice] begingeneratingdeviceorientationnotifications]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (deviceorientationdidchange) Name: Uideviceorientationdidchangenotification Object:nil];
Notification method implementation process:
- (void) Deviceorientationdidchange{ nslog (@ "Deviceorientationdidchange:%ld", (long) [uidevice currentdevice].orientation); if ([Uidevice currentdevice]. orientation == uideviceorientationportrait) { [[ uiapplication sharedapplication] setstatusbarorientation:uiinterfaceorientationportrait]; [self orientationChange:NO]; //Attention: uideviceorientationlandscapeleft and uiinterfaceorientationlandscaperight } else if ([uidevice currentdevice].orientation == Uideviceorientationlandscapeleft) { [[UIApplication sharedapplication] setstatusbarorientation:uiinterfaceorientationlandscaperight]; [self orientationchange:yes]; }}- (void) Orientationchange: (BOOL) landscaperight{ if (landscaperight) { [UIView animatewithduration:0.2f animations:^{ self.view.transform = cgaffinetransformmakerotation (m_pi_2); self.view.bounds = cgrectmake (0, 0, SCREEN_WIDTH , screen_height); }]; } else { [uiview animatewithduration:0.2f animations:^ { self.view.transform = Cgaffinetransformmakerotation (0); Self.view.bounds = cgRectmake (0, 0, screen_width, screen_height); }]; }}// two Macro: #define SCREEN_WIDTH ([UIScreen mainscreen].bounds.size.width) #define SCREEN_HEIGHT ([uiscreen Mainscreen].bounds.size.height)
The most important point:
You need to override the following method and return No.
-(BOOL) shouldautorotate{return NO;
This way, when the device is horizontal screen, the interface will become a horizontal screen, the device is in the vertical screen, the interface will become a vertical screen.
Pits
-
Above mode two, because "general"--"Device Orientation" because only the vertical screen is set, so when the horizontal screen, if there is a keyboard pop-up, the keyboard is the style of the vertical screen.
Workaround: Add the direction of the horizontal screen in "General"--"Device Orientation".
-
If Viecontroller are placed in Uinavigationcontroller or uitabbarcontroller, they need to be rewritten in the direction control method.
uinavigationcontroller:-(BOOL) shouldautorotate{return [Self.topviewcontroller shouldautorotate];} -(Uiinterfaceorientationmask) supportedinterfaceorientations{return [Self.topviewcontroller Supportedinterfaceorientations];} uitabbarcontroller:-(BOOL) shouldautorotate{return [Self.selectedviewcontroller shouldautorotate];} -(Uiinterfaceorientationmask) supportedinterfaceorientations{return [Self.selectedviewcontroller Supportedinterfaceorientations];}
Horizontal screen-(ibaction) Landscapaction: (ID) Sender {[[UIApplication sharedapplication] Setstatusbarorientation: Uiinterfaceorientationlandscaperight]; [Self orientationchange:yes];}
However, following the above notation, the view orientation error will result when returning to the previous interface, even if the following code is executed before returning:
[[UIApplication sharedapplication] setstatusbarorientation:uiinterfaceorientationportrait]; [Self orientationchange:no];
There is no effect, the following is in the open source project unintentionally see the wording:
Horizontal screen- (ibaction) Landscapaction: (ID) sender { [self Interfaceorientation:uiinterfaceorientationlandscaperight];} Vertical Screen- (ibaction) Portraitaction: (ID) sender { [self Interfaceorientation:uiinterfaceorientationportrait];} - (void) Interfaceorientation: (uiinterfaceorientation) orientation{ if ([[ Uidevice currentdevice] respondstoselector: @selector (setorientation:)]) { SEL selector = nsselectorfromstring (@ "setorientation:"); nsinvocation *invocation = [nsinvocation invocationwithmethodsignature:[uidevice instancemethodsignatureforselector:selector]]; [ Invocation setselector:selector]; [invocation settarget:[uidevice currentdevice]]; int val = orientation; [invocation setArgument:&val atIndex:2]; [invocation invoke]; }}
The method above will force the direction of the device to a certain direction, and then monitor the device direction change notification, you can achieve the vertical and horizontal screen switching.
Here is a demo with JS and the original item to control the screen switching. Address
This is:
iOS code handles cross-screen issues