Resize contentSize of UIScrollView with embedded view controller - ios

I have a UIViewController (MasterViewController) with a UIScrollView, that has a header view on top, and a container below.
When I embed any childViewController (e.g. the EmbeddedViewController), the contentSize of the UIScrollView obviously needs to be adjusted. In order to adjust the size, I need to wait until the embedded view is layouted correctly and then set
scrollView.contentSize.height = embeddedViewController.tableView.contentSize.height + HeaderView.frame.height;
When (in the MasterViewController) can I update the scrollView.contentSize after embedding any embeddedViewController?
I thought I could call
[embeddedViewController layoutIfNeeded];
... right before I embed it into the masterView, but somehow the scrollView contentSize is not set correctly. Do you have any ideas or a best practice for dynamic length containerViews in UIScrollViews?

give scrollview contentsize in viewDidLayoutSubviews method
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,350);
}

Related

IOS - Keep UIView on top of UITableView on scroll

I have a UITableView with a UIView on top. I want the UIView to stick to the top as the tableView cells scroll over it.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (self.tableView.contentOffset.y > 0) {
CGRect newframe = self.publicTopView.frame;
newframe.origin.y = -self.tableView.contentOffset.y;
self.publicTopView.frame = newframe;
NSLog(#"After: %f", self.publicTopView.frame.origin.y);
}
}
You need to set your table view header view to the view you want on top.
Add this code to you viewDidLoad
self.tableView.tableHeaderView = self.publicTopView
I'm not certain what you're trying to accomplish, but I have a guess at what is wrong. As you scroll your contentOffset will continue to change and let's say your tableView has a content size height of 1500, then your contentOffset will eventually be larger than the height of your view controllers view. Now see that you are putting that contentOffset into the origin.y of your publicTopView. So your publicTopView could possibly be moving too much, even offscreen depending on how large your tableview's content size is.

UIScrollview: What is setting the contentSize back to 0?

I have a UIScrollView, and in viewDidAppear I set the contentSize:
- (void)viewDidAppear:(BOOL)animated{
CGSize scrollContentSize = CGSizeMake(320, 9200);
self.scrollView.contentSize = scrollContentSize;
}
This code is definitely running.
However, the view doesn't scroll. I wired up a button to log the contentSize, and it returns 0,0. If I get the button to set contentSize again it works fine.
I'm not referencing scrollView anywhere else in my code, what could be setting the contentSize back to 0, and is there any way I can stop it from doing so, or run my setup later in the process of setting up the view?
if you used the Autolayout then you have to make constraints for subviews in it. Then Autolayout will calculate its content size automatically.
But when I set contentsize through button action it works? How?
When the view relayout the scrollview again it will be calculated based on the constraints. So it won't work when u rotate the device/relayout the view.
I don't want to use the autolayout?Now what?
Then you are good to set contentsize.
One more thing, it is best practice to call super implementation on few lifecyle methods.
so do it so.
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// Your code here
}
Here is the Good starting point
Excellent tutorial

UIScrollview is not scrolling

I know there are tens of similar question about scrollview but I have tried everything, Scrollview is not scrolling.
First I created a uiviewcontroller with a xib file, and set it to freeform and arrange the size of it and added a scrollview on view.
Then added it as a childviewcontroller to another view controller.
-(void)scrollViewSetup
{
if (!_scrollView) {
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
[self.view addSubview:_scrollView];
ScrollView *displayPortf=[[ScrollView alloc] init];
//add as a child view controller here
//displayPortf.delegate=(id)self;
[_scrollView addSubview:displayPortf.view];
[displayPortf didMoveToParentViewController:self];
[self addChildViewController:displayPortf];
}
}
Content size of scroll view is (768,200) on iPad.
I tried setting content size to (1920,200) in viewDidLoad didn't work then viewDidAppear didn't work then viewDidLayoutSubviews didn't work.
Then I checked the use auto layout box and tried to add constants by goingeditor->resolve auto layout issues->add constraints
How can I make scrollview to scroll?
Note: Parent view uses auto layout and storyboard.
I guess that the reason of issue is zero width of scrollView.
When you call this:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
_scrollView is initiated with width self.scrollView.frame.size.width, but at this moment self.scrollView is nil as it related to instance variable _scrollView that hasn't initiated yet.
Try to set some non zero float value for view width:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,768,200)];

Dynamically resize UITableView in UIViewController created in Storyboard

In a UIViewController on a storyboard, I have a UITableView that is sized specifically to have two rows in one section with no header or footer, i.e. the height is 88.0f. There are some cases when I want to add a third row. So in viewWillAppear:animated: (and other logical places) I set the frame to be 44.0f logical pixels higher:
CGRect f = self.tableView.frame;
self.tableView.frame = CGRectMake(f.origin.x, f.origin.y, f.size.width, f.size.height + 44.0f);
NSLog(#"%#",NSStringFromCGRect(self.tableView.frame));
Nothing controversial; pretty standard resize code, and yet... It doesn't work! The tableView height doesn't change visually. The NSLog statement reports the height I expect (132.0f). Is this because I'm using Storyboards? I'm not sure why this isn't working.
Set an auto layout constraint for the height of the table view in your storyboard. Then connect the constraint to an outlet in your view controller so you can access the constraint in your code. Have the constraint be set to 88. When you want to change the height of the table view, just change the constraint's constant to 132.
You can modify the frame only after the call to layoutSubviews is made, which occurs after viewWillAppear. After layoutSubviews is called on the UIVIew you can change the dimensions.
As Gavin suggests, if you have the autolayout enabled you can add the constrains to the UITableView via storyboard, connect the height constraint and modify its value as follow:
constraint.constant = 132.0f
Otherwise if you have the autolayout disabled you can simply change the frame updating the height, but putting the code in a different method, for example viewDidLoad:.
recently I'm try to do what you've do. And I got same problem, tableview height won't change. Now I got the solution, you need to call layoutSubviews after change the frame. And it work on me.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height + 44.);
[tableView layoutSubviews];
}
don't place it in viewDidLoad or viewWillAppear: because even layoutSubviews is called, the frame won't change. place it on viewDidAppear:

UIScrollView not scrolling in iOS7 with autolayout on

I have a UIScrollView with a 6 textfields in it and a button inside of it. There is not enough content in the scrollView to make it scroll.
But when the keyboard shows, I would like the scrollview to scroll so the user doesn't have to dismiss the keyboard in order to select another textfield that is hidden by the keyboard.
I am using iOS7 and have autolayout enabled.
Any suggestions?
I am using storyboards and the only code I have is the following.
reg.h file
interface registerViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate>
In order to make a scrollview scrollable, the content size must be larger than the scrollview's frame so the scrollview has something to scroll to. Use setContentSize to adjust the content size:
[scrollview setContentSize:CGSizeMake(width, height)];
In this case, you should adjust the size to view.frame.width, view.frame.height + keyboard_height, then adjust the content offset once the keyboard appears:
[scrollview setContentOffset:CGPointMake(0, 0 - keyboard_height)];
If for some screwy, autolayout-related reason this still doesn't make the view scrollable, implement this setContentSize function in viewDidLayoutSubviews in order to override the autolayout:
- (void)viewDidLayoutSubviews {
[scrollview setContentSize:CGSizeMake(width, height)];
}
EDIT: To reset the scrollview after dismissing the keyboard, reset the scrollview content size to the scrollview's frame and the offset to zero:
[scrollview setContentSize:CGSizeMake(scrollview.frame.size.width, scrollview.frame.size.height)];
[scrollview setContentOffset:CGPointZero];
P.S. To animate the content offset, use:
[scrollview setContentOffset:offsetSize animated:YES];
There is a contentInset property of UIScrollViews, you can set the contentInset to make additional space at the bottom to allow for scrolling without changing contentSize.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 100, 0.0);
scrollView.contentInset = contentInsets;
above code adds 100 points inset at the bottom.
By the way, there is an official document about this matter. It explains everything you should do. You can find it here. You can find what you are looking for under the section 'Moving Content That Is Located Under the Keyboard'
Try
Create Scroll view
Add View to the scroll view (In my case i added view as mainView).
Set ScrollView autoresizing.
Set MainView autoresizing.
To set the Scroll content Size equal to the view created add below line
Add the below line
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.scrollView.contentSize = self.mainView.frame.size;
}

Resources