本文介绍了在Xcode 5.0.1(更新的xcode 6)中为3.5,4.0(更新的4.7和5.5)英寸屏幕开发相同的UI,没有风景,没有iPad,没有故事板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我开发的应用程序考虑3.5英寸的.xib文件而非故事板。

  2. 我找不到任何可以帮助我的教程或指南为所有屏幕设计相似的应用。

  3. 我正在使用Xcode 5.0.1

  4. 我只开发没有自动布局的普通应用,只有适用于iOS 6和iOS 7 (IPhone)。

  5. 以下是截图在不同屏幕上的不同之处。

  1. I have developed app considering 3.5 inch with .xib files and not storyboard.
  2. I am unable to find any tutorial or guide which will help me in designing app similar for all screens.
  3. I am using Xcode 5.0.1
  4. I am developing only plain app with no auto layout and only for iOS 6 and iOS 7 (IPhone).
  5. Below is the screenshot how it differs on different screens.

A。 iPhone iOS 6模拟器:

A. IPhone iOS 6 simulator:

B. 3.5英寸(Ios 6和Ios 7)

C. 4.0英寸(Ios 6和Ios 7)

B. 3.5 Inch (Ios 6 and Ios 7) C. 4.0 Inch (Ios 6 and Ios 7)

更新:
请更新以下答案,了解4.7和5.5英寸屏幕。

Update:Please update below answers to also know 4.7 and 5.5 inches screen.

推荐答案

如果你想检查它以编程方式

用于检查 Retina(3.5 / 4英寸屏幕) )或非视网膜

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] == 2.0) {
       if([UIScreen mainScreen].bounds.size.height == 568){
          // iPhone retina-4 inch
        } else{
         // iPhone retina-3.5 inch
        }
}
else {
    // not retina display
}

更新:

检查 ios8或ios 7

NSOperatingSystemVersion ios8_0_0 = (NSOperatingSystemVersion){8, 0, 0};
 if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_0]) {
    // iOS 8 stuff
 } else {
    // iOS 7 and below stuff
 }

以编程方式检查所有视网膜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 you want to detect the iPhone 6+ only 
           if([UIScreen mainScreen].bounds.size.height == 736.0){
              //iPhone retina-5.5 inch screen(iPhone 6 plus)
           }
           //iPhone retina-5.5 inch screen(iPhone 6 plus)
      }
 }

同时检查此

#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#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)

FOR Swift 3.0

if UIDevice().userInterfaceIdiom == .Phone {
    switch UIScreen.mainScreen().nativeBounds.height {
    case 480:
        print("iPhone Classic")
    case 960:
        print("iPhone 4 or 4S")
    case 1136:
        print("iPhone 5 or 5S or 5C")
    case 1334:
        print("iPhone 6 or 6S or 7")
    case 1920:
        print("iPhone 6+ or 6S+ or 7+")
    default:
        print("unknown")
    }
}

您还可以按如下方式扩展UIDevice:

You can also extend UIDevice as follow:

extension UIDevice {
    var iPhone: Bool {
        return UIDevice().userInterfaceIdiom == .Phone
    }
    enum ScreenType: String {
        case iPhone4
        case iPhone5
        case iPhone6/iPhone7
        case iPhone6Plus/iPhone6splus/iPhone7plus
        case unknown
    }
    var screenType: ScreenType {
        guard iPhone else { return .unknown }
        switch UIScreen.mainScreen().nativeBounds.height {
        case 960:
            return .iPhone4
        case 1136:
           return .iPhone5
        case 1334:
            return .iPhone6/iPhone7
        case 1920:
           return .iPhone6Plus/iPhone6splus/iPhone7plus
        default:
           return .unknown
        }
    }
}






编辑/更新
Xcode 8.1•Swift 3.0.1

extension UIDevice {
    var iPhone: Bool {
        return UIDevice().userInterfaceIdiom == .phone
    }
    enum ScreenType: String {
        case iPhone4
        case iPhone5
        case iPhone6/iPhone7 
        case iPhone6Plus/iPhone6splus/iPhone7plus
        case unknown
    }
    var screenType: ScreenType {
        guard iPhone else { return .unknown }
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhone4
        case 1136:
            return .iPhone5
        case 1334:
            return .iPhone6/iPhone7
        case 1920:
            return .iPhone6Plus/iPhone6splus/iPhone7plus
        default:
            return .unknown
        }
    }
}

可能对您有帮助。

快乐编码。

这篇关于在Xcode 5.0.1(更新的xcode 6)中为3.5,4.0(更新的4.7和5.5)英寸屏幕开发相同的UI,没有风景,没有iPad,没有故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:28