2012-10-02 24 views
8

Ứng dụng của tôi có thêm chức năng cho iPhone 5 và tôi đã tạo một lớp riêng biệt với một .xib cho nó. Tôi muốn phát hiện chiều cao màn hình (trừ khi có thể lấy ID/mẫu thiết bị) và tải một trình điều khiển chế độ xem khác nhau cho phù hợp. Tôi đã thử điều này:Tải một xib khác cho iPhone 5

- (IBAction)select:(id)sender { 

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width; 
CGFloat screenHeight = screenRect.size.height; 

if (screenHeight == 960) { 

Selection *selectView =[[Selection alloc] initWithNibName:nil bundle:nil]; 
selectView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentModalViewController:selectView animated:YES]; 

} 

else { 

    Selection_5 *selectView =[[Selection_5 alloc] initWithNibName:nil bundle:nil]; 
    selectView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:selectView animated:YES]; 

} 

} 

Lựa chọn và lựa chọn_5 là hai lớp khác nhau, mỗi lớp có giao diện người dùng khác nhau.

+0

Có, nó hoạt động. Tôi đã sử dụng float screenHeight = [UIScreen mainScreen] .bounds.size.height –

+0

điều này giúp bạn giải quyết vấn đề này http://stackoverflow.com/questions/13275144/how-to-make-xib-compatible- with-both-iphone-5-and-iphone-4-devices/13283851 # 13283851 –

Trả lời

5

thử http://github.com/erica/uidevice-extension/

[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone 
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G" 

hoặc bạn chỉ có thể xem screenHeight như:

float screenHeight = [UIScreen mainScreen].bounds.size.height; 

cho chiều cao iPhone 5 là 568

và có lẽ bạn shell để thiết nib nếu bạn nạp với an .xib như:

[[Selection alloc] initWithNibName:@"here_is_nibname" bundle:nil]; 
+0

Cảm ơn, sử dụng màn hình nổiHeight = [UIScreen mainScreen] .bounds.size.height đã hoạt động. –

+0

ở chế độ ngang tôi không thể có được chiều rộng chính xác của thiết bị, vui lòng chia sẻ với mọi người ... – Karthik

+0

trong cảnh quan bạn cần so sánh với chiều cao 568, nhưng với chiều rộng 568. –

8

Thứ nhất, bạn không muốn kiểm tra theo loại thiết bị. Điều gì sẽ xảy ra trên iPod touch mới (có màn hình kích thước tương tự) hoặc iPhone năm tới.

Nhưng tôi nghĩ vấn đề ở đây là bạn đang kiểm tra kích thước màn hình dựa trên số lượng pixel thực tế - kỳ quái - không phải là thứ bạn muốn. Hãy nhớ rằng trên màn hình Retina, mọi thứ đều "tăng gấp đôi". Trong giao diện người dùng, bạn (chủ yếu) sử dụng kích thước "bình thường" cho mọi thứ, trong trường hợp này, bằng một nửa số pixel.

Tóm lại: kiểm tra chiều cao màn hình là 480 (bình thường) hoặc 568 (iPhone 5).

+0

Ah, cuối cùng tôi cũng có được 568 tham khảo! Tôi đang sử dụng để tải một xib khác nhau cho các thiết bị 4 " –

5

Trong ứng dụng của tôi, tôi phải tải một tập tin .XIB cho iPhone, iPhone5/iPod Touch và iPad, cho rằng, đây là đoạn code tôi sử dụng:

// If Iphone/iPod Touch 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    // If iPhone 5 or new iPod Touch 
    if([UIScreen mainScreen].bounds.size.height == 568){ 
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewControllerExt" bundle:nil]; 
    ... 
    } else{ 
    // Regular iPhone 
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewController" bundle:nil]; 
    ...   
    } 
// If iPad 
} else { 
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewControllerPad" bundle:nil]; 
    ... 
} 

Hy vọng nó sẽ giúp ai đó mà cần :)

3

Nếu bạn đã có quy ước đặt tên này

VGArticlePage~ipad.xib 
VGArticlePage~iphone.xib 
VGArticlePage~iphone_ext.xib 

Sau đó, bạn có thể làm như thế này

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f) 

- (NSString *)nibNameForClass:(Class)class 
{ 
    if(IS_IPHONE && IS_IPHONE_5) 
    { 
     return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~iphone_ext"]; 
    } 
    else if(IS_IPHONE) 
    { 
     return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~iphone"]; 
    } 
    else 
    { 
     return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~ipad"]; 
    } 
} 
+0

Nếu phương pháp nibNameForClass được đặt như một hàm tĩnh trong một lớp Util, nó có thể được sử dụng ở khắp mọi nơi trong ứng dụng. Tôi thích điều này qua giải pháp của gmogames. – Zuppa

Các vấn đề liên quan