2013-12-12 9 views
7

में आईओएस 7 में अभिविन्यास प्रति दृश्य नियंत्रक को कैसे प्रतिबंधित कर सकता हूं मेरा ऐप एक UITabBarController -> UINavigationController -> UITableViewController -> UIViewController है।मैं नेविगेशन नियंत्रक पदानुक्रम

मैं 2 बातें करना चाहता हूँ:

  1. घूर्णन से मेरी tableview को रोकने के, मैं इसे हमेशा चित्र रहना चाहता हूँ।

  2. फोर्स & मेरे UIViewcontroller को परिदृश्य बाईं ओर घुमाने की अनुमति दें।

मुझे क्या पता:

  1. मैं समझता हूँ कि पदानुक्रम के शीर्ष पर ViewController रोटेशन नियंत्रित करता है। यह मेरा यूआईटीएबीबार नियंत्रक होगा? या इसके बजाय केवल एकमात्र व्यू कंट्रोलर जो ऑब्जेक्ट पर होगा इंडेक्स: 0?

  2. मेरी परियोजना सेटिंग्स पोर्ट्रेट, एलएल और एलआर रोटेशन के लिए अनुमति देती है। मैं सोच रहा हूं कि यह हल करने के लिए मुझे जिस पैटर्न का पालन करना है, उसे हल करने के लिए शीर्ष स्तर पर सभी को अनुमति दें और फिर प्रत्येक वीसी को व्यक्तिगत रूप से नियंत्रित करें, सही?

यही वह है जो मैंने SO में पाया है।

तो मेरे शीर्ष पदानुक्रम के लिए, मैंने पोर्ट्रेट, एलएल और एलआर में रोटेशन की अनुमति देने के लिए प्रोजेक्ट सेटिंग्स सेट की हैं।

और फिर मेरी tableviewcontroller जो मैं घुमाना चाहते न में:

-(BOOL)shouldAutorotate{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

और अंत में मेरे UIViewController में जो मैं बारी बारी से करना चाहते हैं:

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

-(BOOL)shouldAutorotate{ 
    return YES; 
} 

हालांकि यह काम नहीं करता। मैं किसी भी दिशा में दोनों घुमा सकते हैं। मैं यह भी नहीं जानता कि रोटेशन एलएल को कैसे मजबूर करना है जब मैं अपने यूवीसीसी में जाता हूं जिसे मेरे टेबलवीसी से एक मोडल सेग्यू कहा जाता है।

किसी भी बारे में समझने के लिए इस गंदगी बहुत सराहना की जाएगी :)

उत्तर

6

ठीक है, यहाँ यह है। थोड़े जटिल

परियोजना सेटिंग्स की अनुमति देनी चाहिए P, LL & LR

स्टोरीबोर्ड एक UIViewController करने के लिए एक धक्का बार बटन segue के साथ एक UITableViewController के साथ एक UINavController है।

स्टोरीबोर्ड के सभी दृश्यों को अनुरूपित मीट्रिक में अभिविन्यास के रूप में अनुमानित किया जाना चाहिए। बस कह रहा है, थोड़ी देर बाद मुझे परीक्षण के बाद अलग-अलग सेटिंग्स के साथ सब कुछ मिला।

NavController, TableViewController और UIVController के लिए कक्षा होना चाहिए। मेरा ऐप सिंगल व्यू के रूप में शुरू हुआ, फिर मैंने यूआईटीवीसी में खींच लिया और अंत में यूआईटीवीसी में यूआईटीवीसी को एम्बेडेड किया। तब मैंने यूआईवीसी को यूआईटीवीसी बार बटन आइटम से जोड़ा जो मैंने पुश सेगू के माध्यम से खींचा था।

सेट क्षुधा navVC को window.rootvc, अपने शीर्ष पदानुक्रम कुलपति (भूल नहीं स्टोरीबोर्ड में है कि आईडी सेट करने या इसे निश्चित रूप से दुर्घटना जाएगा):

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; 
    UINavigationController *myNavC = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainNav"]; 
    self.window.rootViewController = myNavC; 

बताएँ बड़ा मालिक UINVC हर किसी को बारी बारी से कर सकते हैं:

- (BOOL)shouldAutorotate { 
     return self.topViewController.shouldAutorotate;  
    } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
      return self.topViewController.supportedInterfaceOrientations;  
    } 

तो यह बारी बारी से नहीं होगा tablevc प्रतिबंधित करें:

- (BOOL)shouldAutorotate { return NO; } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
     return (UIInterfaceOrientationMaskPortrait); 
    } 

अंतिम यूआईवीसी को घूमने की अनुमति दें:

- (BOOL)shouldAutorotate { return YES; } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
     return (UIInterfaceOrientationMaskAllButUpsideDown);  
    } 
+2

यह काम किया। लेकिन मुझे यह निराशाजनक लगता है कि एक्सकोड/आईबी इसे स्वयं ही नहीं समझता है और "बस काम करता है"। बेवकूफ रोटेशन बकवास। :/ –

12

सरल लेकिन यह बहुत अच्छा काम करता है। आईओएस 7.1 और 8

AppDelegate.h

@property() BOOL restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
if(self.restrictRotation) 
    return UIInterfaceOrientationMaskPortrait; 
else 
    return UIInterfaceOrientationMaskAll; 
} 

ViewController

-(void) restrictRotation:(BOOL) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 
} 

viewDidLoad

[self restrictRotation:YES]; or NO 
+0

सभी कामकाजों में से, यह केवल मेरे लिए काम करता है .. धन्यवाद। –

+0

समाधान को नियंत्रित करने के लिए सबसे आसान, सबसे आसान –

+0

यदि इसका उपयोग नेविगेशन नियंत्रक में किया जाता है तो इसे देखने के लिए शायद सबसे अच्छा है WillAppear ... अच्छा जवाब हालांकि .. – geekyaleks

0

प्रतिक्रिया

यह यहाँ भ्रमित नहीं होना चाहिए UIDeviceOrientation और इंटरफ़ेस उन्मुखीकरण Appdelegate.h

@property() UIInterfaceOrientationMask restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    UIInterfaceOrientationMask restrictionOrientation = 0; 

if (_restrictRotation == 0) 
    return UIInterfaceOrientationMaskAll; 

UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation]; 

switch (currentOrientation) 
{ 
    case UIDeviceOrientationPortrait: 

     if (!(UIInterfaceOrientationMaskPortrait & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationPortraitUpsideDown: 

     if (!(UIInterfaceOrientationMaskPortraitUpsideDown & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationLandscapeLeft: 

     if (!(UIInterfaceOrientationMaskLandscapeLeft & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationLandscapeRight: 

     if (!(UIInterfaceOrientationMaskLandscapeRight & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    case UIDeviceOrientationUnknown: 

     if (!(UIInterfaceOrientationUnknown & _restrictRotation)) 
      restrictionOrientation = UIInterfaceOrientationMaskAll; 
     else 
      restrictionOrientation = UIInterfaceOrientationMaskPortrait; 

     break; 

    default: 

     NSLog(@"Unknown orientation"); 

     break; 
} 

return restrictionOrientation; 

}

मेरे समाधान

है प्रत्येक वीसी मज़ाक जोड़ें आयन

-(void) restrictRotation:(UIInterfaceOrientationMask) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 

}

viewDidAppear

// 
// Set vc orientation 
// 

[self restrictRotation:UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight]; 
or what you want. 

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"]; 

or what you want. 
संबंधित मुद्दे