I need to align a label in the prototype cell of UITableView to the label outside of the UITableView. When I tried to selected them together and then align horizontally, that option is grey and I can not do that? Then how can I align them so that their center are aligned? Thanks.
-
Can't be done? It's like you have item and price as section header, and the data following can scroll, but they should align with item and price for each entry. maybe the section header can be aligned with prototype cell? – martingale Feb 02 '16 at 04:36
-
Yeah.I was just saying if aligning outside tableview cell is not possible, maybe align the header is possible. My real case is that I need to put the info in header here outside of tableview, cause, user has to input something and there is a button ADD to add those info to each row of tableview. So user need to input item name and price, add to tableview, then input another item name and price and add to tableview and so on., thats why I want this to be out of header a separate view but need to align with the display in tableview. Thanks for your reply. – martingale Feb 02 '16 at 04:59
-
Really appreciate it. The problem is they don't align on different size phone if using same margins/spacing. But thanks for the answer and the answer provided below. I guess I have to choose one. – martingale Feb 02 '16 at 14:18
-
I added a image. and there is no code needed in this case. I don't have enough reputation to add a image. – martingale Feb 02 '16 at 17:11
-
It doesn't look like you are using the same spacing. You should show your view hierarchy and constraints. – Feb 02 '16 at 17:49
1 Answers
To constrain (align) one label with another one, they must be siblings in the same view.
For example, within a tableView cell, you could:
vertically align the left edge of a column of labels
horizontal align the baseline of a row of labels
What you can't do is constrain alignments between labels in two different views (because they don't share a common superview and there would be no way to express a relationship between them). That's why the option is greyed out when you select labels from differing views.
This would include one label in a cell, and another label outside the table, or even in a section header. A cell and a header are two different views within the view hierarchy.
The best you can do is to use the same (leading or trailing) margins for both your header labels and your cell labels, so the cell content coincidently happens to be vertically aligned (to the left or right edge of the header labels).
An alternative approach
Having said that, there are other ways to add a new row to a tableView, if you have decided to do it all within the same view controller.
For example, you could provide inline editing, where you insert a cell with editable item and price textFields, and let the user enter the new information directly into the cell itself.
Again, because the layout could use identical leading or trailing margins, it would be possible to have your content vertically aligned, between displayed content and input content.
This would be much simpler overall, as you could use a tableViewController, instead of a viewController with separate header and table views.

- 1
- 1