2012-03-19 13 views
8

में आईफोन डिवाइस वर्तमान अभिविन्यास कृपया मेरी मदद करें। मैं तरीकों:ViewDidLoad

-(void) getCurrentOrientation{ 

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    if(orientation == UIInterfaceOrientationPortrait){ 
     NSLog(@"portrait"); 
    } else if(orientation == UIInterfaceOrientationLandscapeRight) { 
     NSLog(@"LandscapeRight"); 
    } else if(orientation == UIInterfaceOrientationLandscapeLeft) { 
     NSLog(@"LandscapeLeft"); 
    } 
} 

लेकिन जब मैं इस getCurrentOrientation फोन फेंक viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self getCurrentOrientation]; 

} 

NSLog खाली है। क्या गलत है ? मैं भी

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) { 
     NSLog(@"portrait"); 

    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 

    { 
     NSLog(@"LandscapeRight"); 
    } 

} 

कोशिश लेकिन वह varint भी खाली।

मुझे यह जानने की ज़रूरत है कि कौन सा अभिविन्यास उपयोगकर्ता एपीपी लॉन्च करता है!

कृपया मुझे कोई सलाह दें।

उत्तर

15

आपका कोड बिल्कुल सही है और कोई समस्या नहीं है।

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

यह कथन केवल मूल उपकरणों में काम करता है।

आप जाँच कर सकते हैं लेकिन अगर आप सिम्युलेटर में जाँच करना चाहते हैं के रूप में

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
    // portrait    
} else { 
    // landscape 
} 

अद्यतन:

मैं दोनों डिवाइस और सिम्युलेटर में आप के लिए परीक्षण किया है। प्रारंभ में यह केवल चित्र में दृश्य दिखाएगा हालांकि आप डिवाइस को परिदृश्य में रखेंगे। पहला नियंत्रक चाहिएऑटोरोटेट विधि में देखेंगे।

प्रारंभिक अभिविन्यास के लिए आपको viewDidLoad पर पर निर्भर नहीं होना चाहिए। शुरुआत में सटीक अभिविन्यास के लिए आपको चाहिएऑटोरोटेट विधि पर निर्भर होना चाहिए।

+0

धन्यवाद है। लेकिन जब मैं लैंडस्केप मोड में सिम्युलेटर पर चलता हूं, एनएसएलओएल आउटपुट पोर्ट्रेट। तो मुझे यकीन नहीं है कि यह वास्तविक डिवाइस –

+0

पर काम को सही करेगा, कृपया अद्यतन उत्तर –

+0

देखें, मैं प्रोजेक्ट - लक्ष्य - सारांश - समर्थित डिवाइस ओरिएंटेशन केवल लैंडस्केप में चयन करता हूं। लेकिन जब मैं आपके फ़ंक्शन Nslog आउटपुट पोर्ट्रेट के साथ एमुलेटर में ऐप चलाता हूं। और मुझे क्यों निर्भर नहीं होना चाहिए? मुझे बस यह जानने की जरूरत है कि किस अभिविन्यास में यूजर रन ऐप है। –

2

shouldAutorotate

नीचे
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation 
    { 
     if(interfaceOrientation == UIInterfaceOrientationPortrait){ 
      NSLog(@"portrait");  
     } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
      NSLog(@"LandscapeRight");   
     } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {  
      NSLog(@"LandscapeLeft"); 
     }   
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) 
    } 

इससे आपको यह पता मदद मिल सकती है के द्वारा अपने shouldAutorotateMethod की जगह की कोशिश करो।

+0

बस मुझे बताएं, कृपया, क्यों? मुझे AutorotateToInterfaceOrientation के साथ कोई समस्या नहीं है। मुझे viewDidLoad के साथ समस्या है। –

1

तुम सिर्फ एप्लिकेशन के उन्मुखीकरण की जाँच करने के तो कोड निम्न का उपयोग करना चाहते हैं:

- (void) viewDidLoad { 
    [super viewDidLoad]; 
    BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation); 
    // now do whatever you need 
} 

या

-(void)viewDidLoad 
{ 
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
    { 
     //landscape view code 
    } 
    else 
    { 
     //portrait view code 
    } 
} 
1

अपने viewDidLoad कहा जाता है? क्या आपने वहां ब्रेकपॉइंट लगाया है?

बस NSLog(@"%i", [[UIDevice currentDevice] orientation]) प्रिंट करने के बारे में क्या?

प्रलेखन कहता है कि अभिविन्यास हमेशा 0 देता है, अगर आपने beginGeneratingDeviceOrientationNotifications पर कॉल नहीं किया है। अभिविन्यास प्राप्त करने का प्रयास करने से पहले शायद इसे कॉल करना पर्याप्त नहीं है। कॉल को application:didFinishLaunchingWithOptions: पर ले जाने का प्रयास करें।

हालांकि, सबसे अच्छी बात नियंत्रक द्वारा प्रदान की गई ओरिएंटेशन का उपयोग कर रही है - [UIViewController interfaceOrientation] और पैरामीटर shouldAutorotateToInterfaceOrientation पर पारित किया गया।

+0

प्रतिक्रिया के लिए धन्यवाद। हां अभिविन्यास हमेशा 0 लौटाता है और मैंने (जैसा कि मैंने ऊपर लिखा है) शुरू किया है। प्रारंभ करना डेविसिसऑरेंटेशन नोटिफिकेशन। लेकिन अभिविन्यास भी 0 –

+0

देता है इसलिए मैं कह रहा हूं कि आपको इसे '[यूआईएप्लिकेशंस डिलीगेट एप्लिकेशन: डीफिशिश लांचिंगविथऑप्शन:] ' – Sulthan

+0

धन्यवाद, लेकिन यह विधि अनुपयोगी –

1

जब ऐप पहले लोड होता है, तो UIDevice.current.orientation मान्य नहीं है। लेकिन UIApplication.shared.statusBarOrientation है। UIDevice.current.orientation सामान्य रूप से अभिविन्यास की जांच करने का बेहतर तरीका है, हालांकि। तो, यह विधि सभी स्थितियों को संभाल लेगी

func isLandscape() -> Bool { 
    return UIDevice.current.orientation.isValidInterfaceOrientation 
     ? UIDevice.current.orientation.isLandscape 
     : UIApplication.shared.statusBarOrientation.isLandscape 
} 
संबंधित मुद्दे