I've encountered what I think might be a bug, but I'm not completely sure. So I'm asking you in case you know of a fix.
Basically I have a UIScrollView
that dynamically changes size based on the number of UITextFields
. It starts out at one size (one textfield), and then as more is added, the contentsize of the scrollview increases. This works fine. However, when a user taps a button that opens a UIActionSheet
, the contentsize sets to 0. This makes the scrollview not scrollable when he later dismisses the actionsheet. I placed an NSLog
statement to check the contentsize of the scrollview before and after the actionsheet is presented, and it goes from whatever (for example 360) to 0.
However, this does not happen when I use a UIAlertView
instead. If there isn't a fix for this problem, an alertview is my backup solution. I'd prefer an actionsheet though, as it fits the task better.
I hope someone out there knows what might be causing this and how I can fix it.
Thanks!
As per request, here is some relevant code:
Code for opening the actionsheet:
- (IBAction)addImage:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take a picture", @"Choose from photo album", nil];
[actionSheet showInView:self.view];
}
Here's how I edit the value of the scrollView after a new textfield has been added:
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = CGRectGetMaxY(anotherTextField.frame);
self.scrollView.contentSize = CGSizeMake(width, height);
(Note the anotherTextField.frame
will be the location of the new textfield. So something like 40 - 80 - 120 - 160 etc.)
Here's the viewDidLoad
: (I don't have a viewDidAppear
or viewWillAppear
)
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = CGRectGetMaxY(self.textField2.frame);
self.scrollView.contentSize = CGSizeMake(width, height);
}
It is possible that your UIScrollView
's contentSize is set by an NSLayoutConstraint
. Then an UIActionSheet
causes the constraints to "refresh"...
Edit:
Notice any warnings in interface builder? It usually tells you when the constraints won't be able to find a usable content size for the UIScrollView
. It's actually a pretty complicated issue, this specific issue with UIScrollView
and constraints. I usually do this through code.
The best way here, as you set the contentSize through code anyway, is to just set your UIScrollView
's contentSize in the UIViewController
's - (void)viewDidLayoutSubviews
message.
So instead of
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = CGRectGetMaxY(self.textField2.frame);
self.scrollView.contentSize = CGSizeMake(width, height);
}
Have it like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Any other code here...
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = CGRectGetMaxY(self.textField2.frame);
self.scrollView.contentSize = CGSizeMake(width, height);
}
Or override updateViewConstraints
the same way as a last resort...