0

Is using UIScreen.mainScreen().bounds.size.height to position or size elements in my layout based on device size an acceptable thing to do? Or will this kind of thing have me hauled off to a infinite loop cubical farm for a thrashing?

Sam Luther
  • 1,170
  • 3
  • 18
  • 38
  • Why not position things relative to the bounds of the enclosing view controller? – rmaddy Feb 01 '15 at 01:02
  • Don't know, so say I want to make an element 1/2 its size because its being viewed on an iphone 4, I'd need to know the width or height of the device in order to determine this, yes? – Sam Luther Feb 01 '15 at 01:17
  • 1
    No need to check for an iPhone 4 (or any other device). Simply check the parent view's height. If it is below some threshold, adjust the element as needed. This makes your view controller more flexible. You might show the whole view controller smaller on a larger device, for example. If the layout is based on the screen size instead of the view size, it will be all wrong. – rmaddy Feb 01 '15 at 01:21
  • Thanks for your suggestion. Won't self.view.bounds.size.width be the same value as UIScreen.mainScreen().bounds.size.width? I see the same values when printing them in the console. Sorry if this sounds dumb, bit of a newb – Sam Luther Feb 01 '15 at 01:27
  • @SamLuther you can use `self.view` or `UIScreen`, however they will not always return the same results — for example if `self.view` is inside a navigation controller or split view then it will be smaller than UIScreen. Also t here could be a bug of some kind that makes it any value at all. But UIScreen will always match the screen size perfectly - it's the more reliable option. Both are valid, it's up to you which one to use. UIScreen is the "keep it simple stupid" option. – Abhi Beckert Feb 01 '15 at 01:38
  • 1
    You should almost always use the parent view size instead of using screen size. The former will be significantly more portable, and significantly less likely to break with future versions of the OS and/or future devices. – David Berry Feb 01 '15 at 01:56

1 Answers1

0

Yes it is acceptable, however you do not want to call UIScreen.mainScreen() more than once or twice, as it's a very slow function.

So keep a copy of the result cached like so:

var mainScreen: UIScreen

override init() {
  self.mainScreen = UIScreen.mainScreen()
}

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
  let height = self.mainScreen.bounds.size.height;

  // do stuff with height
}
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • 2
    It is almost never appropriate to size something based on screen size. You should almost always use the size of the destination view instead. – David Berry Feb 01 '15 at 01:53
  • even if i want to actually size something based on the screen size? what is inappropriate about doing that? – Sam Luther Feb 01 '15 at 01:57
  • @David "almost never" is not "never" and I said "acceptable". How do you think I should have worded it differently? With five different screen resolutions available currently on iOS there are times when you need to change your UI depending which one the app is running on, and Apple's recommended approach doesn't always work. A simple `screen height >= x` check is sometimes appropriate. – Abhi Beckert Feb 01 '15 at 01:57
  • 1
    Almost never, you should be using the size of the containing view instead. The only reason for 'almost' is I can't guarantee a time when its not required, but as a general rule you shouldn't be doing it. Is it possible, sure. Is it acceptable, not really. Is it advisable, no. – David Berry Feb 01 '15 at 02:02
  • 1
    @David I've only written two iOS apps and both have situations where using the view size would be buggy. It's more often than you might think. – Abhi Beckert Feb 01 '15 at 02:05