如何仅在iPhone 6 / 6s上显示图像

时间:2016-06-15 17:24:55

标签: ios swift image uiimageview

因此我想在iPhone和6S中使用swift或storyboard在xcode上显示图像。你们有什么建议吗? :)

2 个答案:

答案 0 :(得分:1)

第一步是确定设备是6还是6S。这是Swift代码:

import Foundation
import UIKit

extension UIApplication
{
    public func isIPhone6or6S() -> Bool
    {
        let IPHONE_6 = "iPhone7,2"
        let IPHONE_6S = "iPhone8,1"

        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8 where value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        return ( identifier == IPHONE_6 || identifier == IPHONE_6S) ? true : false;
    }
}

然后你可以在你的UIViewController easily中使用它,就像这样:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var phoneOnlyImage: UIImageView!
    @IBOutlet weak var hiddenStatusLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // hide it if it is not an iPhone 6 or 6S
        self.phoneOnlyImage.hidden = !(UIApplication.sharedApplication().isIPhone6or6S())

        // show the label so you know if it's working
        self.hiddenStatusLabel.text = (self.phoneOnlyImage.hidden) ? "Image is Hidden" : "Image is visible"
    }
}

答案 1 :(得分:0)

enum UIUserInterfaceIdiom:Int         {             case未指定             案例电话             案例垫         }

    struct ScreenSize
    {
        static let SCREEN_WIDTH         = UIScreen.mainScreen().bounds.size.width
        static let SCREEN_HEIGHT        = UIScreen.mainScreen().bounds.size.height
        static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
        static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    }

    struct DeviceType
    {
     /*   static let IS_IPHONE_4_OR_LESS  = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
        static let IS_IPHONE_5          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0 */
        static let IS_IPHONE_6          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0

static让IS_IPHONE_6S = UIDevice.currentDevice()。userInterfaceIdiom == .Phone&amp;&amp; ScreenSize.SCREEN_MAX_LENGTH == 736.0

      /*  static let IS_IPHONE_6P         = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
        static let IS_IPAD              = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
        static let IS_IPAD_PRO          = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0 */

    }

    if DeviceType.IS_IPHONE_6 {
       //Enter Your Code Here