Wrapping UILabel text in UITableView header
I am having a problem getting the UILabel to package correctly in UITableView's
tableHeaderView
. My goal is to have UILabel
one that dynamically expands the height of the heading head UITableView's
based on the amount of text I have on the label.
I am trying to follow the same approach that works when UILabel
in a simple wrapper UIView
. My approach is derived from this tutorial and related project . I manage to repeat the example - my toggle button causes the height of the caption to increase, which increases when pressed. Unfortunately I was unable to implement this in the title UITableView's
.
Figure 1: Single UIView Wrapper (Works)
Below ( UIView
supervisor green, UILabel
gray):
As instructed in the manual, I will subclass the view UILabel's
with an implementation layoutSubviews
that customizes preferredMaxLayoutWidth
, for example:
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat availableLabelWidth = self.label.frame.size.width;
self.label.preferredMaxLayoutWidth = availableLabelWidth;
[super layoutSubviews];
}
Figure 2: UIView outer wrapper (works)
As a next step, I have successfully encapsulated UILabel
in another UIView
to make sure I can extend the outer one as well UIView
. Below (outer UIView
green, inner UIView
in white, UILabel
gray):
External UIView constraints:
I used the same approach layoutSubviews
as above. In this example, both encapsulating heights UIViews
' are adjusted to match UILabel
.
Figure 3: Table Header (Doesn't Work)
Now let's roll this example UILabel
into a table header. I take the same approach as in Appendix 2, except that my outer UIView
is a view UITableView's
tableViewHeader
. Below is shown (table header background, UIView
in green, UILabel
gray):
I am applying the same code layoutSubviews
to the green subclass UIView's
.
This time, the height UILabel's
doesn't change when the content changes UILabel's
. UILabel
and his supervisor stay the same size, and the text is cut off.
I have also included a sample project that contains the three examples described in this post: https://github.com/nsso/flexible-table-header
Does anyone know the reason why my size is UILabel's
not tableViewHeader
resizing and is there anything I can do differently to ensure that the header is sized UITableView's
appropriately?
source to share
No one has answered this question yet
Check out similar questions: