2012-02-06 7 views
7

के भीतर दृश्य आकार की गणना करने के लिए सबसे अच्छा अभ्यास XIB फ़ाइल के बिना loadView विधि (UIViewController में) दृश्य आकार की गणना करने का सबसे अच्छा अभ्यास क्या है?लोड व्यू विधि

यहाँ मेरी समाधान है:

- (void)loadView { 

    //Calculate Screensize 
    BOOL statusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden ]; 
    BOOL navigationBarHidden = [self.navigationController isNavigationBarHidden]; 
    BOOL tabBarHidden = [self.tabBarController.tabBar isHidden]; 

    CGRect frame = [[UIScreen mainScreen] bounds]; 

    if (!statusBarHidden) { 
    frame.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height; 
    } 
    if (!navigationBarHidden) { 
    frame.size.height -= self.navigationController.navigationBar.frame.size.height; 
    } 
    if (!tabBarHidden) { 
    frame.size.height -= self.tabBarController.tabBar.frame.size.height; 
    } 

    UIView *v = [[UIView alloc] initWithFrame: frame]; 
    [v setBackgroundColor: [UIColor whiteColor] ]; 
    [v setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ]; 
    [self setView: v ]; 
    [v release];  
} 

इस कोड को ठीक है, या मैं कुछ संपादित करना चाहिए?

उत्तर

6

डॉक्स [[UIScreen mainScreen] applicationFrame] का उपयोग कर किसी के लिए स्थिति पट्टी

+0

thx, यहां रुचि रखने वाले किसी भी व्यक्ति के लिए दस्तावेज़ हैं: https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/instp/UIScreen/applicationFrame – CarlJ

+0

[मैं कर सकता हूं] (https://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html) – wattson12

+0

इस दावे का आधार क्या है? क्या आपके पास कोई संदर्भ है? – lhunath

0

आप स्थिति बार और नेविगेशन बार पर निर्भर ऊंचाई समायोजित कर रहे हैं .. लेकिन आपने दृश्य की उत्पत्ति के संबंध में कुछ भी नहीं किया है।

+0

हाँ मूल हमेशा होता है {0, 0} । यदि एनवीबार दिखाई देता है तो बिंदु 0,0 Navbar के अंतर्गत है। या मैं गलत हूँ? – CarlJ

1

तो जो एक सबसे अच्छा अभ्यास उदाहरण जानना चाहता हूँ बिना स्क्रीन सीमा प्राप्त करने के लिए सलाह देते हैं:

#pragma mark - 
#pragma mark LoadView Methods 
- (void)loadView { 

    //Calculate Screensize 
    BOOL statusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden ]; 
    BOOL navigationBarHidden = [self.navigationController isNavigationBarHidden]; 
    BOOL tabBarHidden = [self.tabBarController.tabBar isHidden]; 
    BOOL toolBarHidden = [self.navigationController isToolbarHidden]; 

    CGRect frame = [[UIScreen mainScreen] applicationFrame]; 

    //check if you should rotate the view, e.g. change width and height of the frame 
    BOOL rotate = NO; 
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) { 
    if (frame.size.width < frame.size.height) { 
     rotate = YES; 
    } 
    } 

    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) { 
    if (frame.size.width > frame.size.height) { 
     rotate = YES; 
    } 
    } 

    if (rotate) { 
    CGFloat tmp = frame.size.height; 
    frame.size.height = frame.size.width; 
    frame.size.width = tmp; 
    } 


    if (statusBarHidden) { 
    frame.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height; 
    } 
    if (!navigationBarHidden) { 
    frame.size.height -= self.navigationController.navigationBar.frame.size.height; 
    } 
    if (!tabBarHidden) { 
    frame.size.height -= self.tabBarController.tabBar.frame.size.height; 
    } 
    if (!toolBarHidden) { 
    frame.size.height -= self.navigationController.toolbar.frame.size.height; 
    } 

    UIView *v = [[UIView alloc] initWithFrame: frame]; 
    v.backgroundColor = [UIColor whiteColor]; 
    v.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    self.view = v; 
    [v release]; //depends on your ARC configuration 
} 
संबंधित मुद्दे