0

so I would like to make an image only visible on the iPhone 6 and 6S in xcode with swift or storyboard. Do you guys have any suggestions? :)

Peter Boesen
  • 145
  • 1
  • 1
  • 9

2 Answers2

1

The first step is determining if the device is a 6 or 6S. Here's the Swift code to do that:

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:

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"
    }
}
David S.
  • 6,567
  • 1
  • 25
  • 45
  • Hey, sorry, im quite new to this, but could you tell me where to refer to the image i want to hide? – Peter Boesen Jun 15 '16 at 18:31
  • How are you building your UI? Is it in a Storyboard or programmatically? If it's the former, you control-drag an IBOutlet from your storyboard into the UIViewController. – David S. Jun 16 '16 at 01:11
  • i have done that, but i was wondering where is should connect my IBOutlet to the code you gave me :) – Peter Boesen Jun 17 '16 at 13:15
  • Connect it to phoneOnlyImage. I edited the answer to mark it as an IBOutlet – David S. Jun 17 '16 at 13:42
  • again, sry for all the questions, but since you have made the code in two parts, does that mean that the first part should be in the app delegate or something like that? :) – Peter Boesen Jun 17 '16 at 14:52
  • It really depends on where you are using it. If it's only used in the ViewController, put it there. If it's used throughout the app, then it likely should go somewhere else. You could put it in a category on UIApplication, or in your AppDelegate, or a utility class as in the example. – David S. Jun 17 '16 at 15:51
  • okay thank you, i can't make it work yet, but i will play around with it some more. :) – Peter Boesen Jun 17 '16 at 15:53
  • Here's a working example: https://www.dropbox.com/s/vb1oxhek1ana33v/iPhone6Hider.zip?dl=0 – David S. Jun 17 '16 at 18:07
0

enum UIUserInterfaceIdiom : Int { case Unspecified 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

      /*  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
Sai
  • 3
  • 2