UIScrollView does not scroll anymore after the view re-appear - ios

I have a scrollview on the main view after the display of another view the scroll stop scrolling.
On view appear I tried to refresh the scroll but no luck
calling this:
-(void)refreshScrollView{
NSLog(#"scroll Content Height %f",answerScolledHeight );
NSLog(#"scroll View Height %f", scrollView1.frame.size.height );
[scrollView1 setScrollEnabled:YES];
[scrollView1 setContentSize:CGSizeMake(320, answerScolledHeight)];
[scrollView1 setContentOffset:CGPointMake(0, 0)];
}
It reads the right values without working:
2013-03-21 17:56:01.434 ----[38464:14003] scroll Content Height 256.000000
2013-03-21 17:56:01.434 ----[38464:14003] scroll View Height 194.000000
The problem comes after the display of the settings view:
SettingsController* controller1 = (SettingsController *)[self getSettingsViewController];
[self presentModalViewController:controller1 animated:YES];
And when the settings is closed the scroll does not work anymore
- (IBAction)doClose:(UIBarButtonItem *)sender {
[self dismissModalViewControllerAnimated:YES];
}
The viewDidAppear, I try to run some commands the first time to limitate the problems as I am using ECslidingViewController (a wipe menu)
- (void)viewDidAppear:(BOOL)animated{
if (!isActive){
answerUpY = self.answerView.frame.origin.y;
answerDownY = self.questionBackground.frame.origin.y +self.questionBackground.frame.size.height - self.answerToggle.frame.size.height - PADDING_1;
isActive = TRUE;
[self slidingControllerAppear];
}else{
[self refreshScrollView];
}
}

Related

Does UIScrollview works correct with UIStatusbar?

my problem is related with UIScrollview. lets describe it,
I have signup screen which has a scrollview, initially scrolling is not enabled. when keyboard appears I will enable scrollview and when keyboard hides again I am disabling scrolling. my scrollview’s width and height is same as its default view, I have applied horizontal, and vertical centre in container as well as top, bottom, leading and trailing edges are zero (i.e. it is equal to default view). I have a signup button which navigates to signup screen, and applied bottom constraints (bottom space constraints = 0), also I m using keyboard notification for appear and hide.
Actual Problem:
when tap textfield keyboard appears, scrollview scrolls, and when I dismiss the keyboard the scrollview came down, but this time the signup button will move little bit up (like bottom space has 20 points constraints).
First time its like scrollview starts after status bar, but when keyboard appears and hides its like scrollview is renders over view including status bar.
Do I need to add any constraints related to Top/Bottom Layout Guide in IB?
or do I need to add any constraints related to in viewDidLoad
Code for Keyboard notification.
-(void)keyboardWillShow:(NSNotification *)notification {
[self.navigationController.navigationBar setBackgroundImage:nil
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
self.ContentScrollView.scrollEnabled=YES;
NSDictionary *userInfo = [notification userInfo];
CGRect keyboardFrameInWindow;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrameInWindow];
// the keyboard frame is specified in window-level coordinates. this calculates the frame as if it were a subview of our view, making it a sibling of the scroll view
CGRect keyboardFrameInView = [self.ContentScrollView convertRect:keyboardFrameInWindow fromView:nil];
CGRect scrollViewKeyboardIntersection = CGRectIntersection(self.ContentScrollView.frame, keyboardFrameInView);
UIEdgeInsets newContentInsets = UIEdgeInsetsMake(0, 0, scrollViewKeyboardIntersection.size.height, 0);
// this is an old animation method, but the only one that retains compatibility between parameters (duration, curve) and the values contained in the userInfo-Dictionary.
[UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0.0 options:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{
self.ContentScrollView.contentInset = newContentInsets;
self.ContentScrollView.scrollIndicatorInsets = newContentInsets;
/*
* Depending on visual layout, _activeField should either be the input field (UITextField,..) or another element
* that should be visible, e.g. a purchase button below an amount text field
* it makes sense to set _activeField in delegates like -textFieldShouldBeginEditing: if you have multiple input fields
*/
if (_activeField) {
CGRect controlFrameInScrollView = [self.ContentScrollView convertRect:_activeField.bounds fromView:_activeField]; // if the control is a deep in the hierarchy below the scroll view, this will calculate the frame as if it were a direct subview
controlFrameInScrollView = CGRectInset(controlFrameInScrollView, 0, 0); // replace 10 with any nice visual offset between control and keyboard or control and top of the scroll view.
CGFloat controlVisualOffsetToTopOfScrollview = (controlFrameInScrollView.origin.y - self.ContentScrollView.contentOffset.y)+10;
CGFloat controlVisualBottom = controlVisualOffsetToTopOfScrollview + controlFrameInScrollView.size.height;
// this is the visible part of the scroll view that is not hidden by the keyboard
CGFloat scrollViewVisibleHeight = self.ContentScrollView.frame.size.height - scrollViewKeyboardIntersection.size.height;
if (controlVisualBottom > scrollViewVisibleHeight) { // check if the keyboard will hide the control in question
// scroll up until the control is in place
CGPoint newContentOffset = self.ContentScrollView.contentOffset;
newContentOffset.y += (controlVisualBottom - scrollViewVisibleHeight);
// make sure we don't set an impossible offset caused by the "nice visual offset"
// if a control is at the bottom of the scroll view, it will end up just above the keyboard to eliminate scrolling inconsistencies
CGFloat maxScrollViewHeight = MAX(self.ContentScrollView.frame.size.height, self.ContentScrollView.contentSize.height);
newContentOffset.y = MIN(newContentOffset.y, maxScrollViewHeight - scrollViewVisibleHeight);
[self.ContentScrollView setContentOffset:newContentOffset animated:NO]; // animated:NO because we have created our own animation context around this code
} else if (controlFrameInScrollView.origin.y < self.ContentScrollView.contentOffset.y) {
// if the control is not fully visible, make it so (useful if the user taps on a partially visible input field
CGPoint newContentOffset = self.ContentScrollView.contentOffset;
newContentOffset.y = controlFrameInScrollView.origin.y;
[self.ContentScrollView setContentOffset:newContentOffset animated:NO]; // animated:NO because we have created our own animation context around this code
}
}
} completion:NULL];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
[UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue ] delay:0.01 options:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]intValue] animations:^{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.ContentScrollView.contentInset = contentInsets;
self.ContentScrollView.scrollIndicatorInsets = contentInsets;
CGPoint scrollPoint;
self.ContentScrollView.scrollEnabled=NO;
scrollPoint = CGPointMake(0.0, 0.0);
[self.ContentScrollView setContentOffset:scrollPoint animated:YES];
} completion:^(BOOL finished){
__weak typeof(self) weakSelf=self;
[weakSelf.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
weakSelf.navigationController.navigationBar.shadowImage = [UIImage new];
}];
}
Image
If further required I will send the screen shots of before and after keyboard notification.
Thank You.
From Documentation
A Boolean value that indicates whether the view controller should
automatically adjust its scroll view insets.
Declaration
#property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets
Discussion
Default value is YES, which allows the view controller to adjust its
scroll view insets in response to the screen areas consumed by the
status bar, navigation bar, and toolbar or tab bar. Set to NO if you
want to manage scroll view inset adjustments yourself, such as when
there is more than one scroll view in the view hierarchy.
Write self. automaticallyAdjustsScrollViewInsets=NO; in viewDidLoad and try

iOS UITableView content offset iOS7 moving tableview when loading

In my app i have a TableView which has a header that i want to hide initially until the user scrolls the tableview. My problem is when the view loads, for 1 second you can see the tableview moving up. I dont want to see this movement.
How can i achieve this correctly?
This is my code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self performSelector:#selector(hideSearchBar) withObject:nil afterDelay:0.0f];
}
- (void)hideSearchBar
{
self.tblView.contentOffset = CGPointMake(0, 40);
}

CALayer not changing position in UIScrollview

I have a tableViewController with a dynamic UIView inside it, where it's layer position changes so that it stays underneath the status bar. I did the same to a regular View controller with a UIScrollView.
Where the "status banner" in the tableViewController does what it is suppose to, the status banner in the viewController does not.
The status bar's position is modified in a viewDidScroll method within each view controllers.
Why isn't the banner in the second view controller not moving?
Here is the code, it is the same in both view controllers:
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
CALayer *layer = _statusBanner.layer;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset > 10)
{
[[self navigationController] setNavigationBarHidden:YES animated:YES ];
[_statusBanner setHidden:NO];
layer.position = CGPointMake(layer.position.x, scrollOffset + 10);
}
else {
[[self navigationController] setNavigationBarHidden:NO animated:YES ];
[_statusBanner setHidden:YES];
}
}
I figured a workaround with this issue. I had decided to leave statusBanner out of the scrollView of the viewController. The banner and the scrollview are both wrapped in the primary UIView of the viewController and the banner is placed just above scrollView.
I was looking too hard at the problem to see the easiest solution.

UIScrollView child jumping after UINavigationController push and pop

So I have a UIScrollView on my iPad app with a single child view (which itself is parent to all the controls). The scrolling all works fine on it. Rotating works fine (the whole view fits in portrait, scrolls on landscape). Once pushing a new screen on the UINavigationController, and then coming back breaks it.
It looks as if the frame of the scrollview's child has moved up, relative to the scroll position, but the scrollview has remained at the bottom (the entire child view has shifted upwards).
I've tried fighting the Constraints in storyboard, literally for hours, and cannot work out what could be causing this.
I had the same problem with scroll view and auto layouts (iOS 6 - doesn't work, iOS 7 - works fine), of course this is not perfect solution, but seems like it works. Hope it will help you:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self performSelector:#selector(content) withObject:nil afterDelay:0.0];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
offset = self.scrollView.contentOffset;
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
self.scrollView.contentOffset = CGPointZero;
}
- (void)content
{
[self.scrollView setContentOffset:offset animated:NO];
}
Get the frame of the subview before it disappears then manually reset the frame of the subview every time the view appears in -(void)viewWillAppear:(BOOL)animated.
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
globalFrameVariable = subview.frame;
[subview removeFromSuperview];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[subview setFrame:globalFrameVariable];
[scrollView addSubview:subview];
}
Here is a simple solution i found. (Assuming the parent view is meant to span the entire contentSize) Use this subclass of UIScrollView:
#interface BugFixScrollView : UIScrollView
#end
#implementation BugFixScrollView
-(void)layoutSubviews
{
[super layoutSubviews];
UIView *view=[self.subviews firstObject];
if(view)
{
CGRect rect=view.frame;
rect.origin=CGPointMake(0, 0);
view.frame=rect;
}
}
#end
It simply resets the origin every time auto-layout messes it up. this class can be used in InterfaceBuilder simply by changing the class name after placing the UIScrollView.

UIScrollView doen't scroll when is into a UIPageViewController

I'm trying to use a UIScrollView as the content view into a PageViewController, I've configured the Page controller as it is on the sample project of xcode.
The problem is that the content has a Scroll view but it just doesn't scroll at all, I have seted the Content size and the properties for the scroll view.. I guess it is right configured.
- (UIScrollView *)scrollView
{
if (_scrollView == NULL) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.scrollEnabled = TRUE;
_scrollView.alwaysBounceVertical = TRUE;
}
return _scrollView;
}
Then, I'd attached the gestures of my view to be the UIPageViewController gestures array, this was preventing a navigation bar to perform well, so I made a validation on:
gestureRecognizerShouldBegin:
And it is ok now.
I don't know how to fix this issue :S
try this
[scrollmngmt setScrollEnabled:YES];
[scrollmngmt setContentSize:CGSizeMake(320, 1200)];
[self setScrollmngmt:nil];

Resources