简体   繁体   中英

Xcode 6 iOS 7 view size

My root view controller is a tab view controller. My app is landscape mode only. It's set on landscape both in the project settings and in the storyboard for each VC including the root. In viewDidLoad, I have:

NSLog(@"width of view in didappear:%f",self.view.frame.size.width);

and the same code in viewDidAppear. In viewDidLoad in iOS 8, that line prints out 480. In iOs 7, it prints out 320(the width of the screen in portrait mode) but in viewDidAppear, it prints out 480 which is correct. Yes it is the same device for all of you out there who are always in search of 'precise' details.

Why is the view still in portrait in viewDidLoad in ios7 but not in ios8? What can be done? I set up my whole view in viewDidLoad because I only need to do it once and so can't migrate it to viewDidAppear or other places.

It's a curious difference between iOS 7 and 8 if that's the case. I'm guessing it's the same device type but physically different items? Or are you really running iOS 7 & 8 on the same device?

So you could theoretically still have setting differences. Ie iOS 7 device locked in portrait, etc.

If the setup is really as you say, a boolean property would help you to run in viewDidAppear. I just provided an example of how that could be done here: https://stackoverflow.com/a/27867549/1111233

Basically, viewDidLoad is run before the UIElements are loaded and placed on screen, so that's why you are getting the wrong size. As far as the difference between iOS7 and iOS8, I guess apple slightly changed the internals, but in any case you should definitely move your graphics code to viewWillAppear. viewWillAppear is run when the graphics are loaded and works the same as viewDidLoad

use bounds

CGRect rect = [[UIScreen mainScreen] bounds];

NSLog(@"width of view in didappear:%f",rect.size.width);

The frame is relevant to the parent view. The very top view is UIWindow, which in iOS 8 became rotation dependent. In landscape the frame and the bounds will therefore be different in iOS 7, but the same in iOS 8. If you use bounds instead of frame, it should give the same result for both iOS versions, given that the objects are not rotated some number of degrees.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM