How to detect Orientation Change in Custom Keyboard Extension in iOS 8?

后端 未结 10 1916

In Custom Keyboard Extension , we can\'t use

`didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation` 

and

10条回答
  •  我在风中等你
    2020-12-04 18:27

    Non-deprecated and will work on any device screen size (including future screen sizes Apple will be releasing this year).



    In CustomKeyboardViewController.m:

    -(void)viewDidLayoutSubviews {
        NSLog(@"%@", (self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height))) ? @"Portrait" : @"Landscape");
    }
    

    done.










    Or..................

    For a more easy to read version of this code:

    -(void)viewDidLayoutSubviews {
    
        int appExtensionWidth = (int)round(self.view.frame.size.width);
    
        int possibleScreenWidthValue1 = (int)round([[UIScreen mainScreen] bounds].size.width);
        int possibleScreenWidthValue2 = (int)round([[UIScreen mainScreen] bounds].size.height);
    
        int screenWidthValue;
    
        if (possibleScreenWidthValue1 < possibleScreenWidthValue2) {
            screenWidthValue = possibleScreenWidthValue1;
        } else {
            screenWidthValue = possibleScreenWidthValue2;
        }
    
        if (appExtensionWidth == screenWidthValue) {
            NSLog(@"portrait");
        } else {
            NSLog(@"landscape");
        }
    }
    

提交回复
热议问题