2012-02-06 20 views
7

XIB dosyası olmadan loadView yönteminde (UIViewController) görünüm boyutunu hesaplamak için en iyi uygulama nedir? İşte Yükleme boyutunu görüntülemede en iyi yöntem loadView yöntemi

benim çözümdür:

- (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];  
} 

bu kod iyi mi, yoksa bir şey düzenlemek gerekir?

cevap

6

belgeler en iyi uygulama örneği bilmek istiyorum herkes için

+0

thx, ilgilenen herkes için dokümanlar: https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/instp/Uiscreen/applicationFrame – CarlJ

+0

[i can] (https://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html) – wattson12

+0

Bu hak talebini temel alıyorsunuz? Referansın var mı? – lhunath

0

Yüksekliği, durum çubuğuna ve gezinme çubuklarına bağlı olarak ayarlıyorsunuz. Ancak, görünümün kökenine göre hiçbir şey yapmadınız.

+0

evet kökeni daima {0, 0} . Eğer navBar görünürse, 0,0 değeri navBar'ın altındadır. ya da ben hatalıyım? – CarlJ

1

yüzden durum çubuğunda olmadan ekran sınırlarını almak için [[UIScreen mainScreen] applicationFrame] kullanmanızı tavsiye:

#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 
} 
İlgili konular