如果這是一個非常愚蠢的問題,請原諒我,我只是一個初學者。我想爲iPhone 6和6 Plus製作一個單獨的界面,但我不知道如何才能做到這一點。這是一個非常簡單的應用程序,只是爲了讓自己更熟悉該語言等。我想知道是否有一種簡單的方法可以告訴用戶是否使用6或6加上爲他們創建單獨的界面。謝謝:)如何判斷用戶是否使用iPhone 6或6+
回答
在prefix.pch將低於線
#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : 999))))
現在在編程,你可以說...
if (iPhoneVersion==4) {
NSLog("This is 3.5 inch iPhone - iPhone 4s or below");
} else if (iPhoneVersion==5) {
NSLog("This is 4 inch iPhone - iPhone 5 family");
} else if (iPhoneVersion==6) {
NSLog("This is 4.7 inch iPhone - iPhone 6");
} else if (iPhoneVersion==61) {
NSLog("This is 5.5 inch iPhone - iPhone 6 Plus.. The BIGGER");
} else {
NSLog("This is iPad");
}
然而,我會說所有使用一個故事板學習自動佈局的iPhone。
使用'switch'語句。否則,對於iPad而言,您最終會對這一大堆代碼進行四次評估(每個if語句一次)。在iOS 8環境下,代碼將失敗。 – rmaddy 2014-09-26 23:32:03
這是一個相當愚蠢的做法。當下一個iPhone發佈並且代碼錯誤地認爲它是iPad時,它會中斷。 – gnasher729 2014-09-27 12:57:36
@ gnasher729:iPad尺寸的iPhone?偉大的想法...享受編程... – 2014-09-27 12:58:55
使用單個故事板。從raywanderlich瞭解autolayout
,它認爲它是一個非常好的資源。
此外,現在利用在xcode 6中爲ios 8引入的大小類別。這稱爲自適應佈局。
因此,不要分別爲3.5,4,4.7,5.5英寸設備分別製作上述四個視圖,然後考慮將各自的風景模式組合起來,只需使用自動佈局(if可能,強烈推薦),並放鬆任何其他可能會出現的設備尺寸。
檢測iPhone版請使用下面的代碼。
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 667){
// iPhone retina-4.7 inch(iPhone 6)
}
else if([UIScreen mainScreen].bounds.size.height == 568){
// iPhone retina-4 inch(iPhone 5 or 5s)
}
else{
// iPhone retina-3.5 inch(iPhone 4s)
}
}
else if ([[UIScreen mainScreen] scale] == 3.0)
{
if([UIScreen mainScreen].bounds.size.height == 736.0){
//iPhone retina-5.5 inch screen(iPhone 6 plus)
}
}
}
借鑑Here
寫這段代碼.PCH文件
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
和wwhenever你在任何控制器需要你簡單地檢查車況。
下面是做你所問的方法。要注意的一件事是在iOS7及更低版本中,當您檢查[[UIScreen mainScreen] bounds]
的屏幕邊界時,無論手機處於何種方向,它總是列出寬度和高度。因此,如果是處於橫向模式的iPhone5,它仍然會列出寬度爲320,高度爲568.在iOS8中,這種情況發生了變化,現在如果同一個iPhone5處於橫向狀態,它將列出寬度爲568,高度爲320.下面是解釋此方法的方法:
+ (BOOL) deviceHasFourInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0];
}
+ (BOOL) deviceHasFourPointSevenInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0];
}
+ (BOOL) deviceHasFivePointFiveInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0];
}
+ (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height
{
CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
CGFloat mainScreenHeight;
if ([OperatingSystemVersion operatingSystemVersionLessThan:@"8.0"])
{
mainScreenHeight = mainScreenBounds.size.height;
}
else
{
mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height)
{
return YES;
}
else
{
return NO;
}
}
而且,這裏是伴隨OperatingSystem的類方法:
+ (NSString *) currentOperatingSystemVersion
{
return [[UIDevice currentDevice] systemVersion];
}
+ (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare
{
return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);
}
- 1. 如何判斷ID是否是Facebook應用程序或用戶
- 2. 如何判斷用戶是否離線
- 3. 如何判斷soundmanager2是否使用html5?
- 4. 如何判斷Safari是否使用HTTP2?
- 5. 如何判斷是否使用EF5
- 6. 如何判斷是否使用glibc
- 7. 如何判斷用戶是否按下或完成?
- 8. 如何判斷用戶是否使用Brave作爲瀏覽器?
- 9. 使用JodaTime快速判斷當前時間是否在午夜6小時內
- 10. 如何使用jQuery來判斷用戶是否可以滾動?
- 11. 如何打開iOS模擬器iPhone 6/6s或iPhone 6 +/6s +
- 12. 如何判斷Springs和Struts應用程序是否在iPhone 6模擬器中縮放?
- 13. 如何判斷我是否使用ObjectContext API或DbContext API?
- 14. 如何判斷它是否使用zsh或bash
- 15. 如何判斷boto是否使用SSLv3或TLS?
- 16. 如何判斷我們是否使用web1.0或2.0?
- 17. Iphone 6+中的斷點
- 18. 如何判斷此iPhone是否支持Game Center挑戰?
- 19. 如何使應用程序的意見與iPhone 6和6加
- 20. 如何判斷虛擬機是否位於Xen 6虛擬機監控程序
- 21. 如何判斷用戶使用jQuery
- 22. 如何判斷AUCTeX是否可用?
- 23. 如何判斷是否從JUnit調用?
- 24. 如何判斷iPhone應用程序是否由PhoneGap創建?
- 25. 如何判斷用戶是否啓用了「話語提示」?
- 26. 如何判斷用戶是否試圖用C++關閉窗口?
- 27. 如何檢測用戶是否使用iPhone,iPhone 5或iPad
- 28. 如何快速判斷用戶是否觸碰了許多CGRects
- 29. 如何判斷用戶是否選擇不安裝插件?
- 30. 如何獲得iPhone 6或6 Plus的確切屏幕尺寸
使用一個情節串連圖板爲所有iPhone。正確使用自動佈局和約束來將事物放在正確的位置和大小。 – rmaddy 2014-09-26 22:12:51
這篇文章可能也會幫助你:http://stackoverflow.com/questions/25756087/detecting-iphone-6-6-screen-sizes-in-point-values – 2014-09-26 22:16:52