-2

Is it possible to show/hide buttons depending on if the user is using 3.5 inch or 4 inch?

For example, if the view controller is loaded with 3.5 inch the button is hidden, and if it is loaded with a 4.0 inch device the button is shown?

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 4
    This has nothing to do with Xcode. Eve if you used `emacs` and `make` for developing iOS apps, the question and the problem would be the same. –  Jul 26 '13 at 15:45
  • 3
    Trust me. If you had had a look at my profile, you would have quickly found out that I'm a quite established iOS developer. I do have an idea what I am talking about. This is not a problem with Xcode - as I mentioned, the IDE used is irrelevant (I've been developing iOS apps on Linux without any IDE whatsoever), this is a question about the usage of the Cocoa Touch API (and perhaps the Objective-C programming language). Now read the tag wiki for Xcode ([link](http://stackoverflow.com/tags/xcode/info)) to understand that I was right. –  Jul 26 '13 at 15:50
  • see this post about detecting resolution http://stackoverflow.com/questions/12446990/how-to-detect-iphone-5-widescreen-devices – Greg Price Jul 26 '13 at 15:50
  • https://gist.github.com/Jaybles/1323251 Here you can find code to determine the device being used, perhaps this will do for you. – Pieter Gunst Jul 26 '13 at 15:51
  • 2
    H2CO3 is correct, you are asking a runtime question, not a tools question. Xcode is only a tool for building and debugging your application. Of course, the answer to your question is, "Yes, you *could* hide a button based on screen size". Careful about using hard-coded screen size values, as Apple will undoubtably make changes in the future. – gschandler Jul 26 '13 at 15:53
  • Why not just check the bounds of the screen? If the screen has so many pixels, you're on a 4 inch screen. – JeffRegan Jul 26 '13 at 15:55
  • 4
    I'm so tired of reading about the "correct" use of the xcode tag all the time. I mean seriously @H2CO3, you seem to be on some kind of crusade here... And isn't every question that could *potentially* involve Interface Builder actually an Xcode question? Does it really matter? I mean, is anyone seriously following the xcode tag, but somehow not interested at all in Objective-C? – omz Jul 26 '13 at 16:00
  • Thank you so much guys, i learned new things about both stackoverflow and the solution. Great help guys! And thank you for the support omz. – Sebastian Poulsen Jul 26 '13 at 16:03
  • 1
    @omz [Wanna chat about it?](http://chat.stackoverflow.com/rooms/15038/ios-developer-family) –  Jul 26 '13 at 16:05

1 Answers1

0

You can't get the physical size of the screen, but you can get its resolution. The 4" iPhone 5 and the new iPod touch have 320×568 points, while the other iPhones have 320×480. You can get these dimensions using [[UIScreen mainScreen] bounds].size.

It's of course possible that Apple releases a new phone that has the same pixel dimensions as the 4" iPhone 5, but uses a 5" (or whatever) screen, or that they release a device that has completely different dimensions. Your code should ideally be prepared for that, and not make the assumption that these dimensions are set in stone. On the other hand, this kind of change typically doesn't happen overnight.

So, if you want to hide a button on devices with "small" screens, you could do something like this:

- (void)viewDidLoad {
    self.myButton.hidden = [[UIScreen mainScreen] bounds].size.height <= 480;
}
omz
  • 53,243
  • 5
  • 129
  • 141