wrong frame size in viewDidLoad [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why am I having to manually set my view’s frame in viewDidLoad?
I have universal app for iphone and ipad, two storyboards, if I run my app on the iphone simulator in viewDidLoad every frame of each element in nib is correct. But if I do it for ipad (simulator) in frame is nil, but the screen looks correct. Could you please advise me smth? Thanks.

It's a very similar question to ios setContentOffset not working on iPad ... And my answer is the same as there:
"You should not initialise UI geometry-related things in viewDidLoad, because the geometry of your view is not set at this point and the results will be unpredictable.
... viewWillAppear: is the correct place to do these things, at this point the geometry is set so getting and setting geometry-related properties makes sense."
This also applies to getting geometry-related properties. Don't get distracted by it's behaving correctly in one circumstance and not in another... The main point is that setting/getting geometry properties too early will yield unpredictable results.
Your screen looks correct because you are seeing the results after viewWillAppear: / viewDidAppear:.
update (ios6)
When using autolayout in ios6, frames are not set until subviews have been laid out. The right place to get frame data under these conditions is in the viewController method viewDidLayoutSubviews.
This is called after viewWillAppear:. But take care - they are not always called together. For example, viewDidLayoutSubviews is called after a rotation, viewWillAppear: is not. viewWillAppear: is called every time a view becomes the visible view*, viewDidLayoutSubviews not necessarily (only if the views needed relaying out).
(* unless the app was backgrounded and is becoming the foreground app, then viewWillAppear: is not called)

I figured it out why it happened. I was using AutoLayout. When I uncheck it in the storyboard then the frames are correct.

Related

viewDidLayoutSubviews stuck in infinite loop for blank method

I have a view controller that is setup with a UITableView in UIStoryboard. The UITableView is constrained to the SafeArea of the controller on all four sides.
It may seem that this question has been asked before, but each time I have found the question, it is because there are changes taking place in viewDidLayoutSubviews that cause the loop to occur. I have a blank implementation of viewDidLayoutSubviews and basic UITableViewCells (no nib). Yet in iOS 11, Xcode 9.2, flicking the tableview up causes viewDidLayoutSubviews to be called on an endless loop. This is my implementation:
- (void)viewDidLayoutSubviews {
NSLog(#"viewDidLayoutSubviews");
[super viewDidLayoutSubviews];
}
I cannot figure out for the life of me how this is happening.
Edit: The controller has a NavigationController and is the Master controller of a UISplitViewController, which is embedded in a BannerViewController (from iAdSuite example).
Edit: This happens on iOS 11 (up through 11.2) on ALL devices on the simulator. Does NOT happen iOS 10.3. viewDidLayoutSubviews isn't even called in iOS 10.3. I get the same results when I simply use the Nav Controller with the UIViewController/TableView in place of the UISplitViewController/BannerViewController. So seems that the issue is not introduced there, but from something that is happening in iOS 11.
EDIT: So this is odd - seems it is happening only when large titles are selected for the navigation controller!
if (#available(iOS 11.0, *)) {
self.navigationController.navigationBar.prefersLargeTitles = YES;
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
}
EDIT: This problem can be easily reproduced. I downloaded Apple's iAd Suite sample code, and used the SplitNavigationController project. I updated the project for iOS 11, including updating the storyboard to use Safe Area Layout Guides. I changed the MasterViewController from a UITableViewController to a UIViewController with a UITableView, which I constrained to the Safe Area layout guides in the storyboard. I added in prefersLargeTitles and largeTitleDisplayMode to the MasterViewController. I added in the viewDidLayoutSubviews method, with a NSLog statement, so this behavior can be easily observed. I think this is a real problem with iOS 11, and I will try to get to the bottom of it, and I would also appreciate if anyone else can offer any thoughts or suggestions.
EDIT: matt was able to reproduce this issue and post it in a GitHub project, to be found at the following link (thanks matt!): InfiniteLayoutSubviewsBug
EDIT This was definitely a bug in iOS 11 thru at least iOS 11.2, but in iOS 12 the bug no longer exists.
It looks like you've found a bug. I was able to reproduce the problem in a minimal example project, which I've posted at GitHub here:
https://github.com/mattneub/InfiniteLayoutSubviewsBug
Download the project and run it. You'll see a simple table with three rows. Scroll the table view up and let go. Watch the console. We are getting repeated calls to viewDidLayoutSubviews, once every 1/60 of a second, forever.
Note that this is not caused by a recursive layout loop in our code. My example doesn't even call super. All it does is log. This is the runtime itself calling viewDidLayoutSubviews repeatedly forever. No code of ours (except print) runs while this is happening.
Other observations:
As you rightly observed, if you change the example so that the navigation bar doesn't use large titles, the problem goes away.
If you change the example so that the table view is not positioned using autolayout, the problem goes away.
If you change the example (rather more elaborately) so that the view controller is a UITableViewController subclass, the problem goes away; we get some repeated calls to viewDidLayoutSubviews but not forever, just while scrolling.

iOS 8 Custom Keyboard: self.inputView.bounds is still {0,0},{0,0} in viewDidLayoutSubviews in some cases

I'm creating an iOS 8 Custom Keyboard extension primarily using AutoLayout in my XIB. The last step of layout is adding the keyboard keys programmatically. To do this, I need a valid width to properly space keys.
Most StackOverflow posts and references I have read says to perform programmatic layout in viewDidLayoutSubviews. Most posts say that self.view / self.inputView will not have a valid size in viewDidLoad so viewDidLayoutSubviews is the right place to do it.
However, self.inputView.bounds and self.view.bounds (and .frame) give me 0 widths in viewDidLayoutSubviews in some cases.
Oddly, this only seems to happen in iPhone apps that Auto-Scale/Auto-Zoom for iPhone 6's form factor. Zynga's Words With Friends and my old version of Facebook seems to be one of these.
Can anyone help with this? Why would the inputView's bounds/frame ever be 0x0 in by the time viewDidLayoutSubviews gets called? Is there any other better method in which I should add components programmatically?
I don't recommand the using of viewDidLayoutSubviews for keyboard extension. In viewDidLoad it won't have a valid size but you don't need a valid size to do this. Because you know the size of your keyboard as long as you get the device model (orientation is another thing).
As for your question, viewDidLayoutSubviews along with viewWillLayoutSubviews is sometimes called many times. And frame.bounds.width == 0 is also possible when the view is created. That's why I don't recommand using them. The reason I think is that the keyboard extension part of iOS 8 is still buggy and unstable.
If you really need to use viewDidLayoutSubviews, prevent it from getting wrongly called:
- (void) viewDidLayoutSubviews {
if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0)
return;
// Do something
}

Who should manage the UI Elements, the view or the view controller?

I am currently developing an iOS app and I was wondering how to manage the UI elements. I am using a Storyboard to place my views and Autolayout to make everything resolution independent. If it helps, here's a bit of background on how I came up with this question.
Some Backgroud
I have buttons the represent piano keys and I later in code add a subview to these that draws the actual keys on them. I did this by creating #IBOutlets on the ViewController and susbscribed to the UIDeviceOrientationDidChangeNotification. This called a method that adds the subviews based on the current button frames to get the appropriate sized keys.
This worked fine when running in iOS 8.1, but if I ran the app on iOS 7, the frames of the buttons weren't updated to the new orientation by the time I got the UIDeviceOrientationDidChangeNotification, so everything was messed up in landscape. I did a little research (UIInterfaceOrientation not yet updated when UIDeviceOrientationDidChangeNotification caught in UIView) and it seems that a good solution would be to override the layoutSubviews method. Problem is, layoutSubviewsis a UIView method, and I am managing my buttons and UI in my UIViewController.
Impulsively I wanted to subclass my UIView, override layoutSubviews and from there call a method on my Controller to add the keys to the buttons correctly, but that doesn't sound right.
The Questions
My UI is managed by the UIViewController. But I need to update my views based on a method proper of a UIView. I assume, to respect the MVC principles, that my view should't now a thing about my controller, but then this brings up a few questions:
Who should be managing the UI?
Is my strategy wrong and should the
UIView hold the #IBOutlets to the buttons so that it can later
apply the subviews I need them to have?
If you read the background, do you have a suggestion for this particular situation?
I assume the complexity of this problem is relatively small and I don't need to setup notifications in NSNotificationCenter, but I may be wrong. I would really like to hear this is not the only solution.
Thanks in advance. I hope I was clear enough, but if you want/need any additional details on my particular situation, let me know and I will gladly elaborate.
if I understood the question correctly, your ViewController should be laying out the keys within it's view. A good, but not the only, place to do this is in viewDidLayoutSubviews which a method you can override on UIViewController.
Now if your views should maintain their internal layout. i.e if you buttons/keys have any subviews, you should update those in layoutSubviews on UIView as you mentioned.
Both methods will be called in response to changes in the bounds or center of your parent view.
As far as rotation changes are concerned, in iOS 8, you should use viewWillTransitionToSize:withTransitionCoordinator: or willTransitionToTraitCollection:withTransitionCoordinator:
depending on your needs.
I highly recommend that you write your layout independent of orientation. You should just use the containing view's bounds for reference and never hard code in any frames. For example perhaps one of your keys should be 1/56 the width of it's superview instead of a magical number for portrait and landscape.

Get width and height of container from view

Forgive me if this is a duplicate. I thought for sure this would be an obvious question.
I have a container in a storyboard, which is of class ModelViewController (the whole view controller). From ModelViewController I need to get the width and height of the container. I am currently trying:
self.view.bounds.size
Which yields 0.563380 and 0.000000. I also tried
self.view.frame.size
Which yields the dimensions of the entire screen. What am I missing here?
From this post
View frames are not actually useable in viewDidLoad; you should move all of your geometry-manipulating code into viewWillAppear. The system will clobber any changes you set in viewDidLoad between there and when it's about tom come on screen.
And the comment
Actually, you should move geometry changes to -viewDidAppear pre-iOS 5 and to -viewWillLayoutSubviews for iOS 5+. -viewWillAppear: may not actually have the correct bounds/orientation if the view is being animated.
I found the solution. The correct dimensions are indeed given when called from viewWillLayoutSubviews. I was calling from viewDidLoad giving incorrect results.

Bounds of custom UIView not correct during awakeFromNib - when are they set?

I have a custom UIView, which I have placed using Xcode (4). I need to set some default state, based on the actual bounds of the view. During awakeFromNib, bounds seems to be returning the size of the view in the storyboard layout in Xcode.
The view is in the detail side of a UISplitViewController, which in Xcode is the size of a full portrait iPad screen, but if the app loads in landscape mode then, via springs-and-struts, its size is changed, but this appears to happen after awakeFromNib.
Should I be setting this state in some other method?
It depends what sort of state you are setting - if it is dependent on the bounds then you'll need to reset it every time the device is rotated, presumably? In that case overriding setFrame: might be a better bet - be sure to call the superclass implementation before you do anything else, though.
The answer is was probably looking for, or at least the solution I have used, is to provide a public method in the UIView to be called by the parent UIViewController in viewWillAppear:

Resources