Resize subviews in table view when editing using auto layout - iphone

I have a table view with prototype cells. Currently I have only added a label to the cell. When enabling edit mode, a delete button will appear animating the cell smaller but the label doesn't reposition. This used to be no problem with autoresizing masks but now I can't seem to get it to work.
Does anyone know how I should configure auto layout to have the label positioned inside the cell when in edit mode?

I searched some more and found that it is necessary to remove the horizontal spacing constraint that Interface Builder adds, as this is relative to the UITableViewCell and not the contentView and then add a constraint which is relative to the content view.
You can read more about this in this answer to a similar issue.

Related

Nested UIStackViews in UITableViewCell not working with constraints

I have a simple table view controller. Section 0 will be a single cell with a couple of labels, textfields, and a few buttons. I wanted to use stack views to layout the fields neatly. The moment I add any constraints I get the whole thing off the cell and not visible. The one button that is alway visible isn't even the correct width. The cell height is plenty large enough to hold it. It looks perfect in the interface builder, and has no errors or conflicting constraints. What am I missing?
Then image with the fields all there is without any constraints.
In the Interface builder
ScreenShot

Autolayout a UIScrollView to fit content including subviews and grouped tables

I am trying to present information about an object grouped into sections. It may be long, so each section uses a view to visually separate the areas and it is all in a scroll view.
The first subview has a text field that does resize to fit the text, and its parent view also resizes to fit the text field.
The second subview has a grouped table to display data. I want all of the data in the table to appear and there should be no table scrolling.
The third subview has more information but its contents are an empty view for the purposes of this demo. In the real app I may add an arbitrary number of custom subviews.
What I'm finding is that the first subview resizes appropriately but I cannot find the combination of layout constraints that will make the second subview size to fit the table and move the third view down.
What do I need to do to make the scroll view fit its contents, when the sub view's contents may resize? The table view is filling the available space in the parent view, but the parent view is not expanding to fit. I note that the third view has a top space constraint to the superview, not just to the view above it. That's visible in the screenshot below
I found a solution that like most solutions, seems obvious in retrospect.
I think my problem is that the interior tableview had no fixed height and since it also a scrollview, was trying to fit whatever was assigned instead of expanding to fit the interior content. I was able to solve my problem by adding a fixed height constraint in IB and hooking it up to an outlet in my view controller. Then in the controller:
-(void) viewWillLayoutSubviews{
[self.tableView layoutIfNeeded];
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height;
}
Derp. In summary the table view needed a height so I set one.
Here's a screenshot, note the fixed height constraint on the right hand side:

Custom cell in UITableView

I am making a custom cell which has text fields and as a result the custom cell is greater than the size of the iphone screen.But i am not being able to do horizontal scroll to reach the end of the cell.
I have tried using viewController and adding table view to it as well as creating table view controller and adding custom cell to it.
when doing it through TableViewController,I am not able to horizontally scroll it,whereas i am not getting how to add Custom cell class to the tableView object placed in viewController.I know that UITableView is a subclass of UIScrollView but not able to implement it.Please help.
Thanks
as per the documentation at
A table view in the UIKit framework is limited to a single column
because it is designed for a device with a small screen. UITableView
is a subclass of UIScrollView, which allows users to scroll through
the table, although UITableView allows vertical scrolling only.
so as desired in your case you will have to take some other approach. Now using Custom cell with TextFields should not actually need you to require Horizonatl scrolling. If the text is really large you would like to consider using TextView instead of TextFields.
Hope it helps
Ideally, you should be truncating your text. But if it is important for you to display the entire text, add a scroll view as a subview (or set it as the content view) and add your labels to the subview. Each cell will then be individually horizontally scrollable.

Custom UITableViewCell for a grouped table view

I have a grouped table view.
I'm trying to create a custom cell using interface builder, however when I'm putting a label, or a view as a subview for the cell, and stretching it to the entire cell, when I run the app the label goes beyond the cell's dimensions.
Any reason why it would do that ?
I tried to play with the resizing mask to no avail.
When the table is plain, there's no problem.
I guess I'm doing something wrong, cause it's not suppose to be that complicated.
I don't have much experience in creating UITableViewCells with IB, but I would recommend adding your subviews programmatically using a UITableViewCell subclass. Your issue is that the subviews are added directly to the UITableViewCell view, and the left and right margin are part of that view.
The UITableViewCell subview that represents the actual "active" space (the white part of the cell) is contentView. If you add a subview to contentView, then it shouldn't appear outside it. In other words, CGPointZero of contentView is the top left point of the white space.

UITableViewCells of different heights place their accessoryViews at different X positions

My app has some table cells that vary in height. The cells can also have a UIButton set to be a detail disclosure button (round, blue with arrow) as their accessory view.
Depending on the height of the cell, the accessory view is positioned differently. At first I thought it was my layout code for my cell that was causing the problem, so I set up a quick independent test that uses vanilla UITableCells to remove the possibility that it could be my fault.
I set up a view in interface builder, and just added a view table cells to the view, set their heights to different values and then added a detail disclosure button to each. Nothing more, nothing less.
This is what I see:
UITableViewCells with different x values http://jasarien.com/jing/accessoryView_x_difference.png
I added the size guides (thanks to Xscope) so you can see the difference in the accessory view x positions.
The heights are:
top 37px
mid 68px
bottom 44px (default, untouched height)
If I increase the height any heigher than 68px the accessory view doesn't move any further to the left.
Is this a bug? Is there any way I can prevent this from happening?
Here's the test project to reproduce.
TableViewCellHeightsTest.zip
I got the same problem when I downloaded your file. Instead of setting the detail disclosure buttons manually and assigning them to cells as outlets, delete all disclosure indicators and try setting them this way instead:
Note: I set the background color of the content view to blue for ease of view.
Figure 1 (accessory view height is 17.0)
I was facing this problem and had the luxury of 2 colleagues helping me figure out the cause.
We find out that when using the default UITableViewCell (In my case, of style UITableViewCellStyleDefault though I believe it applies to all other styles), if your accessory view's height is anything above the magic number 16.0, the x position of the accessory view start to differ with the variance of height of the cell.
Figure 2 (accessory view height is 16.0)
My colleague had implemented a custom UITableViewCell and using subviews to layout content and was able to avoid this problem.
So you have 3 options:
Restrict your accessory view's height to 16.0 and below.
Use a custom UITableViewCell and layout your own content as subviews.
Use the default accessory type.
Figure 3 (default accessory type UITableViewCellAccessoryDetailDisclosureButton)
I was having the same issue and fixed it by making the custom accessory view the same height as the cell.