iOS UITableView content offset iOS7 moving tableview when loading - ios

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);
}

Related

ios objective-c tableView searchBar

I have a hidden searchBar in a tableView header:
- (void)viewWillAppear:(BOOL)animated {
CGRect newBounds = self.tableView.bounds;
newBounds.origin.y = newBounds.origin.y + self.searchBar.bounds.size.height;
self.tableView.bounds = newBounds;
}
I also have a magnifying-glass icon button that reveals the search bar:
- (IBAction)showSearchBar:(id)sender {
[self.searchDisplayController setActive:YES animated:YES];
[self performSelector:#selector(showKeyboard) withObject:nil afterDelay:0.1];
}
Problem is, when I pull down the table (to reload data) - it also shows the search bar.
I only want the searchBar to become visible when the magnifying-glass icon gets tapped.
I am not really sure, how to address this problem?
Am I hiding it the wrong way in the first place?
Thanks,
Added a screenshot to make my question more clear:
searchbar appears when pulling down
I don't tried that out, but I can image that you just have to hide it. for example:
[[self.searchDisplayController view] setHidden:YES]

UISearchDisplayController table view overlapping search bar

I have a UITableViewController subclass, displayed in a modal view on an iPad. The view controller has a UISearchDisplayController subclass with a UISearchBar included in the header view of the table.
The subclassed UISearchDisplayController is called NoAnimationSearchDisplayController and I have overridden the - (void)setActive:(BOOL)visible animated:(BOOL)animated
method to prevent the search bar from animating into the navigation bar when it's set to active. The method override is below...
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if (self.active == visible) {
return;
}
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
The problem I'm having, is that when I have searched, and results are displayed in my search display controller's table view, everything looks fine untill i try to scroll down the list, at this point the content within the cells appears above the search bar, as shown in the following screen:
The search bar is set to UISearchBarStyleMinimal and transparency is enabled. Can anyone let me know how to stop this content overlapping the search bar? Ideally the content would disappear under the search bar as if it was the end of the view.
The answer was to manually change the frame of the table view provided by the UIsearchDisplayController in the appropriate delegate method...
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
/**
* Remove content inset automatically set by UISearchDisplayController as we are forcing the
* search bar to stay in the header view of the table, and not go into the navigation bar.
*/
[tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
/**
* Recalculate the bounds of our table view to account for the additional 44 pixels taken up by
* the search bar, but store this in an iVar to make sure we only adjust the frame once. If we
* don't store it in an iVar it adjusts the frame for every search.
*/
if (CGRectIsEmpty(_searchTableViewRect)) {
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y + 44;
tableViewFrame.size.height = tableViewFrame.size.height - 44;
_searchTableViewRect = tableViewFrame;
}
[tableView setFrame:_searchTableViewRect];
}
Be sure the search bar is not translucent enabled ( on storyboard/xib ) or by code.
Then make the background to white or whatever color you want.

How to reset my UIScrollView's position after returning from a modal transition?

I have a simple view containing a long view with many buttons, with the whole thing being in a UIScrollView. The scroller works well, and I can scroll to the bottom and click a button. Every button triggers a modal seque to another view. That new view is then dismissed by user interaction, causing the original UIScrollView's view to load again.
Here's the problem: If I click on a button toward the top of the UIScrollView, I enter the modal segue, dismiss the new view, and return to the UIScrollView's view without a problem. But, if I click on one of the buttons toward the bottom of the UIScrollView, when I return seque out and then transition back, my scrolling is all messed up. I can only see the area beneath my scroller, and can't scroll back up to the top anymore!
I'm pretty sure there must be some way to reset the UIScrollView's starting and ending points upon ViewWillAppear, but I can't figure it out. Any help is appreciated!
Also, FYI, I simply added the UIScrollView through interface builder, and haven't implemented or synthesized it anywhere yet.
try this code:
- (void)viewWillAppear:(BOOL)animated
{
[yourscrollview setContentOffset:CGPointZero animated:YES];
}
Please note: the bug this question and answer is about appears to be fixed in iOS 7. The rest of this answer is only relevant to iOS 6 (and probably earlier).
The behaviour being exhibited here is a bug in the UIScrollView class. As noted by the OP, after returning from a modally presented UIViewController to a scene containing a UIScrollView, the UIScrollView takes whatever point it's currently scrolled to and starts behaving as though that is its origin. That means that if you'd scrolled down your scroll view before modally presenting another View Controller, you can't scroll back up upon returning to the scene with the scroll view.
The same thing happens when you remove the Scroll View from the view hierarchy and re-add it, even without changing its window.
You can work around this by setting the contentOffset of the scroll view back to {0,0} before it gets displayed again after dismissing the modal View Controller. If you actually want to preserve the point the user had scrolled to before they triggered the modal, then after the UIScrollView is redisplayed you can set the contentOffset back to whatever it was before you reset it.
Here's a UIScrollView subclass that fixes the bug without resetting the scroll view to the top whenever you return from a modal:
#interface NonBuggedScrollView : UIScrollView
#end
#implementation NonBuggedScrollView {
CGPoint oldOffset;
}
-(void)willMoveToWindow:(UIWindow *)newWindow {
oldOffset = self.contentOffset;
self.contentOffset = CGPointMake(0,0);
}
-(void)willMoveToSuperview:(UIView *)newSuperview {
oldOffset = self.contentOffset;
self.contentOffset = CGPointMake(0,0);
}
-(void)didMoveToWindow {
self.contentOffset = oldOffset;
}
-(void)didMoveToSuperview {
self.contentOffset = oldOffset;
}
#end
If you'd rather do this in a UIViewController than in a UIScrollView subclass, change the content offset in the viewWillAppear: and viewDidAppear methods.
If you don't want to preserve where the user's scroll position when they return from a modal, and just want to scroll the UIScrollView back to the top, as the OP asked for, then all you need is the even simpler:
#interface NonBuggedScrollView : UIScrollView
#end
#implementation NonBuggedScrollView
-(void)willMoveToWindow:(UIWindow *)newWindow {
self.contentOffset = CGPointMake(0,0);
}
-(void)willMoveToSuperview:(UIView *)newSuperview {
self.contentOffset = CGPointMake(0,0);
}
#end
First, thanks for the approved answer above. Someone mentioned that it was no longer applicable but I have a scrolling view inside of table view cell and it needs to be reset when the cell is reused.
Here is the solution in Swift.
#IBOutlet var scrollView: UIScrollView!
// many lines of code later inside a function of some sort...
scrollView.setContentOffset(CGPointMake(0.0, 0.0), animated: false)
To solve this problem i use this code:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.scrollview scrollRectToVisible:CGRectMake(0, 0, 1, 1)
animated:NO];
}
You can change the starting and ending points by calling scrollRectToVisible:animated:. But I'm not sure if this fixes your problem.
Use below code snippet to restore the scroll position for a UIScrollview
Declare "scrollPosition" variable as CGPoint type.
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//get the current offset
scrollPosition = scrollView.contentOffset;
//set current view to the beginning point
self.scrollView.contentOffset = CGPointZero;
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
//retrieve the previous offset
self.scrollView.contentOffset = scrollPosition;
}

UIScrollView does not scroll anymore after the view re-appear

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];
}
}

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.

Resources