I have an expandable UITableView, which can expand/collapse when users press a button.
The way is that I change its content when the button is pressed, and assign the contentSize.height to the height of the tableview. It works properly in iOS 6.
However, in iOS 7, no matter what value I set to the height of the tableview, it displays like the height is never changed and the new content is cut off. The height seems to stick to the origin height in the storyboard. But if I print the height of the tableview, it's right the value I set.
Here is how I change its height:
//Do some change to the content...
[tableview reloadData];
NSLog(#"height: %f, contentHeight: %f", tableview.frame.size.height, tableview.contentSize.height);// print height: 60, contentHeight: 160
CGRect frame = tableview.frame;
frame.size.height = tableview.contentSize.height;
tableview.frame = frame;
NSLog(#"height: %f, contentHeight: %f", tableview.frame.size.height, tableview.contentSize.height);// print height: 160, contentHeight: 160
Any ideas?
p.s. The tableview is in a UIScrollView. (if it matters.)
I think your problem is the scrollview.
try something like this:
CGRect screen = [[UIScreen mainScreen] bounds];
CGSize scrollViewContentSize = CGSizeMake(screen.size.width, screen.size.height);
[self.scrollView addSubview:tableview];
scrollViewContentSize.height += tableview.frame.size.height;
[self.scrollView setContentSize:scrollViewContentSize];
[self.view addSubview:self.scrollView];
Related
In my previous question I shared my problem with the black background appearing in my app on orientation change: Black background when view rotates on orientation change
I did not manage to solve the problem by following any of the advices I got and I have the feeling that the only way I can avoid the black background is by manually rotating my subviews on orientation change?
One of my subviews is a UILabel which is supposed to cover the entire screen. The rotation is going pretty well using a line of code similar to this one:
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
My problem is to make the UILabel adjust to take up the entire screen in landscape mode as well. I have tried to switch height and width of its bounds, but nothing happens.
Any suggestions will be greatly appreciated.
Here are some more code details:
[UIView animateWithDuration:0.5
animations:^{
myLabel.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(isLandscape ? 90 : 0));
}
completion:^(BOOL finished) {
myLabel.bounds = CGRectMake(0, 0, [self screenWidth], [self screenHeight]);
CGFloat fontSize = ((isLandscape ? 0.9649 : 0.9375) * [self screenWidth]) / 2.74;
[myLabel setFont:[UIFont fontWithName:FontName size:fontSize]];
}
];
where
- (CGFloat) screenWidth {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MAX(screenSize.width, screenSize.height) : MIN(screenSize.width, screenSize.height);
}
- (CGFloat) screenHeight {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
return isLandscape ? MIN(screenSize.width, screenSize.height) : MAX(screenSize.width, screenSize.height);
}
Perhaps you are calling sizeToFit on the label somewhere?
Try instead to set:
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumFontScale = 0.1;
This will adjust your labels font size to fit the width of label.
I found out that I was able to adjust the label size once I unchecked the auto layout checkbox.
So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.
I want to change the height of a textview depending on its content. I created the method to resize its view. The first time I call this view controller it resize properly (height = 253) but not the other times (height = 296).
I tried resizing it from viewDidAppear, viewWillAppear and viewDidLoad. The first time viewDidAppear and viewDidLoad are called. Second and following times all methods (viewDidAppear, viewWillAppear and viewDidLoad) are called. I don't know the reason and I don't know why this weird behavior, any clue?
-(void) setHeight
{
NSLog(#"Set height");
CGRect frame = descriptionTextView.frame;
frame.size.height = descriptionTextView.contentSize.height;
descriptionTextView.frame = frame;
NSLog(#"height: %f", descriptionTextView.frame.size.height);
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
totalHeight = 380+frame.size.height;
[self.mainScrollView setContentSize:CGSizeMake(320,totalHeight)];
}
else{
totalHeight = 250+frame.size.height;
[self.mainScrollView setContentSize:CGSizeMake(320,totalHeight)];
}
}
I use autolayout in my project but not for this view since I dont know how to properly resize a textview inside a scrollview (which also includes 2 more views with labels, images and buttons) based on the textview content with autolayout. Is it better to use autolayout than this function? Perhaps you can help me with the constraints...
You are changing only the scroll size according to your calculation but you are missing to change the size of text view itself. After setting scroll view content size update the size of the text view and all should be fine (contentSize and frame). This may look like:
[descriptionTextView setContentSize:CGSizeMake(CGRectGetWidth(descriptionTextView.frame),totalHeight)];
CGRect frame = descriptionTextView.frame;
frame.size.height = totalHeight;
[descriptionTextView setFrame:frame];
By the way: in the if-statement you can only calculate totalHeight and set it afterwards.
Additionally you do not need this line:
frame.size.height = descriptionTextView.contentSize.height;
Try this:
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = yourTextView.frame.size.width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"yourFontName" size:yourFontSize], NSFontAttributeName,
nil];
CGRect frame = [yourTextView.text boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
// Use this frame for your textview
I have a storyboard with a view, but all my UIViews (actually UIViewTable and UIScrollView) appear to have a frame.width and frame.height of 0.
I can clearly see them in the simulator.
- (void) viewDidAppear:(BOOL)animated{
// check frame size of tableView
NSLog(#"Frame width = %f, height = %f", self.tableView.frame.size.width, self.tableView.frame.size.height);
// check contentSize of tableView
NSLog(#"contentSize width = %f, height = %f", self.tableView.contentSize.width, self.tableView.contentSize.height);
// check frame size of scrollView
NSLog(#"scrollView size - width = %f, height = %f", self.scrollView.frame.size.width, self.scrollView.frame.size.height);
[super viewDidAppear:animated];
}
All these logs show 0 for the height and the width.
I am trying to resize UIScrollView inside a UICOllectionViewCell but it doesn't resize it.
-(int)resizeScrollView:(int)height{
NSLog(#"Before Scroll height %f",self.scrollView.frame.size.height);
float currentHeight=self.scrollView.frame.size.height+height+200;
NSLog(#"height %f",currentHeight);
CGSize size=CGSizeMake(self.scrollView.frame.size.width ,
currentHeight + 10 );
[self.scrollView setContentSize:size];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSLog(#"After Scroll height %f",self.scrollView.frame.size.height);
return currentHeight;
}
Setting the contentSize adjusts the amount of logical space that the scroll view scrolls over. You need to set the frame or the bounds to adjust the size of the scrollview in its parent view.
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height += height + 210;
self.scrollView.frame = scrollFrame;