I have a view controller I made. There is a lot of information displayed on it, including labels and images. I want to set it so if the device used is the iPhone 4 then an image does not display, to help conserve the view controller real estate. How do I do this in Swift?
Asked
Active
Viewed 1,183 times
2 Answers
1
For detecting devices (like iPhone 4):
var device = UIDevice.currentDevice().model
This string would show the device model. For instance, iPhone3,1
represents that it is an iPhone 4.
You can find the string for each device (including iPod and iPad) in this page: UIDevice currentDevice model possible values
if device == "iPhone3,1" {
imageview.hidden = true
}
The above method only works on real devices, but not simulators.
If you only want to hide the UIImageView on certain screen sizes regardless of the device type, you can use the method below.
if UIScreen.mainScreen().nativeBounds.height == 960.0 {
// code for iPhone 4 or 4S
} else {
// code for the rest
}
Checking the width like what Leonardo Savio Dabus answered would not work, since iPhone 5 has the same width as iPhone 4, but iPhone 5 has a larger screen than iPhone 4.

Community
- 1
- 1

Henry Ngan
- 572
- 1
- 4
- 24
-
He could simply use if statement to find out if the string is, for example, iPhone3,1. If yes, then he could set UIImageView.hidden = true – Henry Ngan Dec 15 '14 at 20:23
-
If he only wants to hide the UIImageView on iPhone 4, then he would need to include 3 strings in if statement. `if device == ("iPhone3,1" || "iPhone3,2" || "iPhone 3,3") {...}` This would be more customisable since he might want to display images on iPhone 4s since iPhone 4s would have more processing power (assuming that he hides imageViews on iPhone 4 because it would be laggy) – Henry Ngan Dec 15 '14 at 20:30
-
Sorry I accidentally published my unfinished answer before. You can look at my edited answer above. – Henry Ngan Dec 15 '14 at 20:33
-
What I've noticed is that the iOS simulator does not recognize this? Is there anyway to test this despite not actually having this device to test? – ChallengerGuy Dec 15 '14 at 22:10
-
The only way to make it work using the simulator would be checking the native bounds of your device. – Leo Dabus Dec 15 '14 at 22:46
-
@ChallengerGuy There I updated the answer for you. :) – Henry Ngan Dec 16 '14 at 10:46
0
You can use this code to detect the device during runtime:
var device = UIDevice.currentDevice().model

Stefan
- 5,203
- 8
- 27
- 51