简体   繁体   English

如何使图像仅在iPhone 6 / 6s上可见

[英]How to make an image visible only on iPhone 6/6s

so I would like to make an image only visible on the iPhone 6 and 6S in xcode with swift or storyboard. 因此,我想使用swift或Storyboard在xcode中使图像仅在iPhone 6和6S上可见。 Do you guys have any suggestions? 你们有什么建议吗? :) :)

The first step is determining if the device is a 6 or 6S. 第一步是确定设备是6S还是6S。 Here's the Swift code to do that: 这是执行此操作的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;
    }
}

Then you can use it in your UIViewController easily enough, something like this: 然后,您可以在UIViewController easily足够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"
    }
}

enum UIUserInterfaceIdiom : Int { case Unspecified case Phone case Pad } 枚举UIUserInterfaceIdiom:Int {case未指定案例Phone case Pad}

    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 let IS_IPHONE_6S = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0 静态让IS_IPHONE_6S = UIDevice.currentDevice()。userInterfaceIdiom == .Phone && 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM