iosiphoneadaptive-ui

Acceptable to use UIScreen.mainScreen().bounds.size.height to programmatically position elements?


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?


Solution

  • 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
    }