2012-02-06 31 views
7

Phương pháp hay nhất để tính kích thước khung trong phương thức loadView (trong UIViewController) không có tệp XIB?Cách thực hành tốt nhất để tính kích thước khung trong phương thức loadView

Đây là giải pháp của tôi:

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

là mã này sao, hay tôi nên sửa cái gì?

Trả lời

6

Các tài liệu khuyên bạn sử dụng [[UIScreen mainScreen] applicationFrame] để có được những giới hạn màn hình mà không thanh trạng thái

+0

thx, đây là tài liệu dành cho bất kỳ ai quan tâm: 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

Bạn căn cứ vào khiếu nại này như thế nào? Bạn đã có một tài liệu tham khảo? – lhunath

0

Bạn đang điều chỉnh độ cao phụ thuộc vào thanh trạng thái và thanh điều hướng .. Nhưng bạn chưa thực hiện bất kỳ điều gì liên quan đến nguồn gốc của chế độ xem.

+0

có nguồn gốc luôn là {0, 0} . Nếu navBar hiển thị thì điểm 0,0 nằm dưới navBar. hay tôi nhầm? – CarlJ

1

để cho bất cứ ai muốn biết một ví dụ thực hành tốt nhất:

#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 
} 
Các vấn đề liên quan