iOS code handles cross-screen issues

Source: Internet
Author: User

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];}
    • If you want to click a button, force the vertical screen to become a horizontal screen?
      One might think of writing this:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.