Width of multiline UILabel in UITableView keeps shrinking with autolayout - ios

I have a UILabel functioning as a title in my UITableViewCell. It displays fine the first time it is used, but each time it is redisplayed the width shrinks. All my layout is done in IB, nothing in code at the moment.
[EDIT]
The left hand side of the label is pinned to the (barely visible) box that says "New Words". The right hand side is pinned to the superview. Height is set to 44px.
I am able to fix this by using the following code in layoutSubviews. I don't like it at all though since it involves setting the frame. I would like to understand the underlying problem.
[super layoutSubviews];
CGRect frame = self.titleLabel.frame;
CGSize size = self.titleLabel.intrinsicContentSize;
frame.size.width = size.width;
frame.size.height = 44;
self.titleLabel.frame = frame;
[super layoutSubviews];
Here are a few screenshots of the problem:
First time:
Second time:
Third+ time:

Related

How to make UIScrollView height equal to content height?

I have an UIScrollView and horizontally scrollable content. I can properly set up the content size, etc, but I need to place another similar ScrollView just below the first one. So, I need to make height of the ScrollView to be equal to height of its content. Currently I use this code, but it does not work. I attached screenshot with issue and the code:
//setting the correct content size of scrollView (self.contentSize is already calculated)
self.imageScrollView.contentSize = self.contentSize;
//creating temporary CGRect
CGRect frame = self.imageScrollView.frame;
//Changing the height and re-assigning new frame to imageScrollView
frame.size.height = self.contentSize.height;
self.imageScrollView.frame = frame;
Look at the screenshot: the imageScrollView is just below NavBar. The content still scrollable and the size of ScrollView is smaller than content size.
You can't set the frame's height using frame.size.height = self.contentSize.height;
Instead use frame = CGRectMake(x, y, width, height); Ex:
self.imageScrollView.frame = CGRectMake(self.imageScrollView.frame.origin.x, self.imageScrollView.frame.origin.y, self.imageScrollView.frame.size.width, self.contentSize.height);
And from the looks of things, I suspect the content size of your imageScrollView is correct, but it's being hidden by your nav bar. In which case, I recommend setting imageScrollView frame's origin to the height of the nav bar, ex:
self.imageScrollView.frame = CGRectMake(self.imageScrollView.frame.origin.x, self.navigationController.navigationBar.frame.size.height, self.imageScrollView.frame.size.width, self.contentSize.height);
Update #1: Here's a link to a great answer explaining why you can't set the frame in the way you've attempted: ios frame change one property (eg width)
So basically you can edit a lone CGRect structure by changing a single property, but you can NOT update a CGRect in that way if it's a property of another object -- in this case a property of a frame.
Update #2: Also, to change the position of elements in your interface, place the code in viewDidLayoutSubviews, not in viewDidLoad.
Update #3: To have the code in viewDidLayoutSubviews only processed once, enclose it in a conditional, ex:
bool subviewsLaidout;
- (void)viewDidLoad {
subviewsLaidout = NO;
}
- (void)viewDidLayoutSubviews {
if (!subviewsLaidout) {
self.imageScrollView.frame = CGRectMake(self.imageScrollView.frame.origin.x, self.navigationController.navigationBar.frame.size.height, self.imageScrollView.frame.size.width, self.contentSize.height);
subviewsLaidout = YES;
}
}

How to make UITextView so high that it exactly fits the text content on iOS7?

I have a UITextView that's embedded into a UIScrollView. I would like the text view to not scroll and be exactly as high as required to show all of the text.
So the width is fixed and I set the content insets to indent the text a bit.
How do I get the correct height? I tried to set the frame's height to the content height but still it scrolls.
This should do the trick:
-(void)resizeTextView
{
CGRect frame = _textView.frame;
frame.size.height = [_textView sizeThatFits:CGSizeMake(frame.size.width, INFINITY)].height;
_textView.frame = frame;
}

UIScrollView Move Resizes UITextView

I have a strange issue, I am using IOS6 and I am trying to get a UIScrollView to resize based upon the content within it, there are 3 UITextView (2 fixed and 1 dynamic height). The problem seems to be that when you scroll down the scroll view the UITextView with the dynamic height will revert back to the previous height set in Interface Builder. I have placed the code below to show how I am setting the scroll height and UITextView height.
- (void)viewDidAppear:(BOOL)animated {
CGRect frame = description.frame;
frame.size = description.contentSize;
description.frame = frame;
CGFloat scrollViewHeight = 0.0f;
scrollViewHeight = 160 + description.contentSize.height;
NSLog(#"%f",description.contentSize.height);
[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
}
To expand it seems that the UITextView will not expand into the scrollable area of the UIScrollView, instead it only goes to the initially set height of the scrollview
It turns out that it was the layout constraints that were causing the issue.

Set the width of an grouped cell in Xcode

Am fooling around with this question a couple of days now but no progress. What i want to do is quite simple i think:
I have an image of 320x60 which i use in the plain TableView which works oke as those cells take up the entire width (320) of the screen. The grouped cells in a TableView are 300 wide and have insets/margins left of 10 on the left and the right.
Can i somehow remove those insets/margins and let the grouped cell be 320 wide? I tried setting the content inset left to -10. That does "remove" the left margin but then it's still only 300 wide. Also tried editing the XML of the storyboard (I'm working with iOS 5 - Storyboards) but no joy.
This similar question here got answered as no it's not possible, hopfully something changed in 2+ years!:
Adjust cell width in grouped UITableView
PS i want to alter the width as the background images contain nice shadows, I've read that exesive use of shadows could mean performance issues. Also the shadow's are 5px extra around the border so that would mean -10px wide if I use the standard width.
Help much appreciated!
An untidy solution is to make the table view 340 pixels wide, and 10 pixels off the left edge of the screen.
A solution that involves changing properties of private classes is to make a UITableViewCell subclass, and override its layoutSubviews method. When I log the subviews, I find these:
"<UIGroupTableViewCellBackground: 0x95246b0; frame = (9 0; 302 45); autoresize = W; layer = <CALayer: 0x95226b0>>",
"<UITableViewCellContentView: 0x92332d0; frame = (10 0; 300 43); layer = <CALayer: 0x9233310>>",
"<UIView: 0x95248c0; frame = (10 0; 300 1); layer = <CALayer: 0x951f140>>"
What happens if we take those subviews and fill the entire bounds available?
- (void)layoutSubviews;
{
// By default the cell bounds fill the table width, but its subviews (containing the opaque background and cell borders) are drawn with padding.
CGRect bounds = [self bounds];
// Make sure any standard layout happens.
[super layoutSubviews];
// Debugging output.
NSLog(#"Subviews = %#", [self subviews]);
for (UIView *subview in [self subviews])
{
// Override the subview to make it fill the available width.
CGRect frame = [subview frame];
frame.origin.x = bounds.origin.x;
frame.size.width = bounds.size.width;
[subview setFrame:frame];
}
}
At this particular moment, on the iOS 5.1 simulator, this works. Now, some future version of iOS may restructure these classes, causing this method to catastrophically mangle the table layout. Your app could be rejected for changing the properties of UITableViewCellContentView... even though you're only modifying its frame. So, how much do you need to have your cells fill the table width?
You can the UITableView's Leading and Trailing Space constraints in the Size Inspector which is accessible via the Storyboard. I'm not sure when this was added, but setting the Leading Space Constraint to -10 and the Trailing Space Constraint to 10 will make the cells full width.

UIScrollView content size clipping subviews when it shouldn't

I have a UIScrollView that contains several dynamically resizing subviews. I can resize and layout the subviews just fine, but when I set the content size of the scroll view itself, the bottom subviews are clipped. Is there some reason why a scroll view's content size height should be larger than the sum of the heights of the views it contains?
Here's my situation in more detail:
I have a superview containing a UIScrollView containing several subviews. In the superview's layoutSubviews method, I calculated the needed size of each subview, then set the frames so the subviews are tiled vertically down the screen with a bit of space between them. When done, I set the height of the UIScrollView's content size to be the end of the last subview (origin.y + size.height). In theory, this means the bottom of the scroll view's content area should exactly line up with the bottom of the last subview.
But it doesn't. Instead, a nice chunk of the last subview is clipped. It's still there - if I scroll down I can see the remaining portion during the "bounce". The problem is even worse in landscape mode - a much larger portion of the bottom subview simply isn't visible.
The subviews are all being arranged and positioned properly. The problem is that the UIScrollView's contentSize seems to need to be significantly larger than the sum of the heights of the subviews (plus the space between them). This doesn't make any sense to me. Furthermore, the amount the size is "off" varies - I reuse this view several times with different subviews, and they're all off by a different amount. Therefore, simply adding a constant to the content view height won't help.
What is causing the content size (or my height calculations) to not function correctly?
Code:
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.bounds.size.width - [self subviewLeftMargin] - [self subviewRightMargin]; // All subviews have same width as parent view
CGFloat x = [self subviewLeftMargin]; // All subviews should start at the far left of the view
CGFloat y = [self spaceBetweenSubviews]; // Running tally of the y coordinate for the next view
/* Adjust the subviews */
for(UIView *view in self.theVariousSubviews) {
/* Resize the view with the desired width, then let it size its height as needed */
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, width, view.frame.size.height);
CGSize newSize = [view sizeThatFits:view.frame.size];
/* Set the origin */
//The subviews are positioned correctly, so this doesn't seem to be a problem
view.frame = CGRectMake(x, y, newSize.width, newSize.height);
/* Have the view refresh its own layout */
[view setNeedsLayout];
/* Update the y value for the next subview */
y += newSize.height + [self spaceBetweenSubviews];
}
/* Resize the scroll view to ensure it fits all of the content */
CGFloat scrollViewHeight = y;
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, scrollViewHeight);
//Content size is set to the same total height of all subviews and spacing, yet it is too small. Why?
}
hi it seems to me that your calculation and resizing timing is wrong.
Without the missing code for the layout change I could not fully understand the problem.
What strikes me is that you are assigning view.frame twice and between the new calculation you intercept the process with sublayouting which might change some of the values your calculation is depending on.
I could only advice you to separate the calculation from layouting and not invoke methods while you are calculating. To bring light into it you should either drop a sample app with the missing calculation or for yourself add some NSLog statement showing you the frame origin size of any subview and the contentOffset for the scrollview.
On my experiences the scrollview is working properly in general so I would expect a bug within your code.

Resources