iOS - Main view resizes - additional UIViews do not - ios

In my XCode project, I have a xib file and the main view resizes for the iPhone 5 (as do its subviews).
I have additional UIViews in this Xib (they're not set as subviews of the main view, though) and these ones don't resize even though I have set the auto sizing properties.
Am I doing something wrong or is there a reason that these additional UIViews aren't resizing?
Thanks.
EDIT: I am adding these views as subviews to a parent view in my viewdidload method. Maybe I should do it in viewWILLload?

Seems I've managed to figure it out! You have to set the frame of the UIViews in viewWillAppear to be whatever size you want and then the subviews will resize.
Luckily, in my case, all my tall narrow views that you can see in the screen grab above, are all going to be the height of the screen, so I can set it there and have the subviews position/resize accordingly.

Related

autolayout constraints in nib not honored for UITableViewHeaderFooterView

I have a UITableView with several columns of data. The reusable cells are loaded from an xib file which has the appropriate labels and autolayout constraints. Everything works perfectly; the table columns are laid out correctly on different devices and when the devices are rotated.
The problem I am having is trying to create a table footer to show the totals of the columns in the table.
I created an xib file with the same autolayout constraints as the cell xib file and am loading it in tableView.viewForFooterInSection the same way I did for the cells. As required, I am using a subclass of UITableViewHeaderFooterView instead of UITableViewCell.
The awakeFromNib method in the UITableViewHeaderFooterView subclass sets the background color, so I can see that it is the correct size on all devices/orientation, but the labels from the footer xib file are not getting laid out to match the table cells.
The autolayout constraints from the footer xib file are not being honored. When I set a different background color in the footer xib file, the table footer shows this background color for the length of the xib's view.
I'm new to all of this technology and would greatly appreciate help in resolving this incredibly frustrating issue.
Is there a way to use autolayout for UITableViewHeaderFooterViews loaded from nibs?
You should call setNeedsUpdateConstraints() to update your view.
From apple documentation
Controls whether the view’s constraints need updating.
When a property of your custom view changes in a way that would impact constraints, you can call this method to indicate that the
constraints need to be updated at some point in the future. The system
will then call updateConstraints as part of its normal layout pass.
Updating constraints all at once just before they are needed ensures
that you don’t needlessly recalculate constraints when multiple
changes are made to your view in between layout passes.
Also, you can update view throw layoutSubviews()
Lays out subviews. The default implementation of this method does
nothing on iOS 5.1 and earlier. Otherwise, the default implementation
uses any constraints you have set to determine the size and position
of any subviews. Subclasses can override this method as needed to
perform more precise layout of their subviews. You should override
this method only if the autoresizing and constraint-based behaviors of
the subviews do not offer the behavior you want. You can use your
implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a
layout update, call the setNeedsLayout method instead to do so prior
to the next drawing update. If you want to update the layout of your
views immediately, call the layoutIfNeeded method.

Best time in View lifecycle to determine the on-screen size of a UIScrollView

I have a .xib containing a view which itself containing top and bottom accessory views (navigation, a page control) and a UIScrollView. Its height varies depending on whether I'm running on an iPhone 4 or 5 (3.5" or 4"). At runtime I move the containing view offscreen, populate the scrollview with UIButtons to build a scrollable launchpad, and scroll this on to the screen from the bottom. Tapping a button shows another view.
On a smaller screen I'd like to show only three buttons in the scrollview, on the larger one I show four. So I need to know the height that the scrollview will be when it appears, before it actually appears. I'd hoped to have this information available at some point in the view lifecycle (viewWillAppear etc.) but the only place this seems to be correct is in viewDidAppear. At this point the view is already on screen and creating the buttons then means they appear suddenly, and are not scrolled nicely onscreen.
The top-level view in the .xib is set to Retina 4 FullScreen.
I'm happy to concede that viewWillAppear is the correct place to create the buttons and my navigation controller is perhaps instantiating the view incorrectly, but my iOS-fu isn't strong enough to say for certain.
My solution has been to detect screen size and hardwire the button height. Is there a better (more elegant, future-proof and canonical) way to do this?
Don't worry, you are in the right direction:
viewWillAppear is the correct method in a view's lifecycle, where the frames of that view and its subviews are completely calculated and nothing has been drawn yet. Here you can make corrections to the standard behavior of your view.
When viewDidAppear gets called, you get the same information about positions and sizes of view elements as in viewWillAppear. But at this time, the animations and the view itself already have been drawn.
That is, why you'll want to use viewWillApear.

How do I get the size for a UICollectionView cell when layoutSubviews hasn't been called

I'm trying to create a UICollectionView where the cells are varied sizes based on the contents within them and how those contents play with the constraints set on them. Basically, I need to examine the frame size but this size isn't updated on the cell until after the subviews have been laid out. Unfortunately, this isn't done until the view is attached to the window hierarchy it seems. I can't find any series of calls to trigger this without adding it to the window hierarchy, yet I don't want to add it to the hierarchy except through this UICollectionView. And, of course, the collection view wants the size of it before it requests and adds the cell.
I can't get to size without adding it to the window hierarchy (I think), and I can't get it added to the window hierarchy (i.e. the collection view) without having the size. What am I missing here?
Just call these on the cell, and it should act as though it was added to the view hierarchy:
setNeedsLayout()
layoutIfNeeded()

Why UITableView on the device smaller than in IB

When I create my UI in IB I can't do it right because UITableView height not equals its height on the device.
I don't understand why?
Example:
http://img35.imageshack.us/i/20110401111017.jpg
This is happening because the view is being resized when it is pushed on the navigation stack. You can fix this several ways. The best thing to do it setup your nib so it matches the displayed size. You can also remove te autoresize flags from the UITableView. Finally you could programmatically resize the view inside of the view controller's viewWillShow:(BOOL)animated.

iPhone + resize UITableView

I have a view in which I have UITableView (grouped style). In the Interface builder, I am resizing the UITableView so that it looks like a small portion in center of the screen.
But when I run the application, UITableView takes up the whole area of screen and does not look like the small portion in center of screen (which I had set in Interface builder).
I tried to resize the tableView programmatically in the viewDidLoad method as tableView.frame = CGRectMake (0.0,0.0,100.0,100.0), but still the tableView occupies the whole area of screen.
Please help.
Regards,
Pratik
Sounds like your table view's got autoresized. Try to fiddle with the autoresizing settings.
If your table view is the main view then it will automatically fill the whole view controller's space regardless of the autoresizing settings. In that case, make an empty UIView as the root view and put the UITableView as a subview of it.

Resources