2014-09-14 12 views
12

एप्लिकेशन एक्सटेंशन में डिवाइस के अभिविन्यास का पता लगाने का सबसे अच्छा तरीका क्या है? मैं मिश्रित परिणाम के समाधान के साथ मैं यहाँ पर मिल गया है पड़ा है:ऐप एक्सटेंशन में अभिविन्यास का पता लगाने का सबसे अच्छा तरीका क्या है?

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

Get device current orientation (App Extension)

मैं आकार वर्गों और UITraitCollection देख कर पाया है डिवाइस गलत ढंग से रिपोर्ट है कि है कि यह चित्र जब यह कर रही हैं वास्तव में परिदृश्य में है (सुनिश्चित नहीं है कि यह ओएस बग है, या मैं सही एपीआई सही तरीके से पूछताछ नहीं कर रहा हूं)।

पूरा करने के लिए सबसे अच्छा तरीका क्या है:

  • उपकरण के वर्तमान उन्मुखीकरण जब एक्सटेंशन पहले भरी हुई है
  • उन्मुखीकरण डिवाइस
  • उन्मुखीकरण के लिए बारी बारी से होगा डिवाइस
  • को बारी बारी से किया

धन्यवाद,

उत्तर

18

मुझे उस समस्या का सामना करना पड़ा मी और आपके उदाहरणों को भी देखा, लेकिन इसका कोई अच्छा समाधान नहीं था। मैंने इसे कैसे हल किया: मैंने एक कक्षा बनाई जो यूआईस्क्रीन मानों की कुछ गणना करता है और स्वयं परिभाषित डिवाइस अभिविन्यास देता है।

कक्षा हैडर:

typedef NS_ENUM(NSInteger, InterfaceOrientationType) { 
    InterfaceOrientationTypePortrait, 
    InterfaceOrientationTypeLandscape 
}; 

@interface InterfaceOrientation : NSObject 

+ (InterfaceOrientationType)orientation; 

@end 

कार्यान्वयन:

@implementation InterfaceOrientation 

+ (InterfaceOrientationType)orientation{ 

    CGFloat scale = [UIScreen mainScreen].scale; 
    CGSize nativeSize = [UIScreen mainScreen].currentMode.size; 
    CGSize sizeInPoints = [UIScreen mainScreen].bounds.size; 

    InterfaceOrientationType result; 

    if(scale * sizeInPoints.width == nativeSize.width){ 
     result = InterfaceOrientationTypePortrait; 
    }else{ 
     result = InterfaceOrientationTypeLandscape; 
    } 

    return result; 
} 

@end 

मैं इसे viewWillLayoutSubviews या viewDidLayoutSubviews तरीकों के लिए रखा पकड़ने के लिए अभिविन्यास में परिवर्तन घटना।

if([InterfaceOrientation orientation] == InterfaceOrientationTypePortrait){ 
    // portrait 
}else{ 
    // landscape 
} 

मामले में आप इस दृष्टिकोण आपकी समस्या का समाधान नहीं होगा डिवाइस अभिविन्यास (छोड़ दिया, ठीक है, उल्टा) की एक सटीक पक्ष प्राप्त करना चाहते हैं। यह सिर्फ पोर्ट्रेट या लैंडस्केप उन्मुखता देता है।

आशा है कि यह आपकी मदद करेगा।

+0

मुझे लगता है कि कोड भाग गया और यह और यह परिदृश्य लौटे डिवाइस वास्तव में चित्र था जब? मैं अभी भी आईओएस 8 अभिविन्यास परिवर्तन के बारे में उलझन में हूँ। जहां तक ​​मुझे पता है कि फ्रेम नहीं बदलता है लेकिन सीमाएं अभिविन्यास के साथ होती हैं। अब यह सिर्फ सम्मानित है या? – user519274

+0

उपयोगकर्ता 519274 - टिप्पणी के लिए धन्यवाद। यह अभी भी काम करता है। क्या आपने कोड को सही जगह पर रखा था? यह UIViewController के WillLayoutSubviews या ViewDidLayoutSubviews विधियों को देखने में होना चाहिए। –

+0

लेकिन डिवाइस अभिविन्यास कैसे प्राप्त करें भूमि हैस्केप लाइफ या लडस्केप राइट इस तरह की एक http://stackoverflow.com/questions/25462091/get-device-current-orientation-app-extension –

1

आप UIApplicationWillChangeStatusBarOrientationNotification पर एक पर्यवेक्षक जोड़कर विस्तार में डिवाइस अभिविन्यास प्राप्त कर सकते हैं और फिर निम्नानुसार अभिविन्यास निकाल रहे हैं।

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil]; 
} 

- (void)orientationWillChange:(NSNotification*)n 
{ 
    UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue]; 

    if (orientation == UIInterfaceOrientationLandscapeLeft) 
     //handle accordingly 
    else if (orientation == UIInterfaceOrientationLandscapeRight) 
     //handle accordingly 
    else if (orientation == UIInterfaceOrientationPortraitUpsideDown) 
     //handle accordingly 
    else if (orientation == UIInterfaceOrientationPortrait) 
     //handle accordingly 
} 

धन्यवाद

संबंधित मुद्दे

 संबंधित मुद्दे