iOS hiding navigation bar due to scroll amount - ios

I've tried to implement such a table view that detects scroll amount and decides to show navigation bar or do not.
#interface HomeViewController () {
NSInteger scrollAmount;
bool navbarHidden = NO;
}
#implementation HomeViewController
#synthesize lastContentOffset = _lastContentOffset;
bool navbarHidden = NO;
- (void)awakeFromNib
{
[super awakeFromNib];
scrollAmount = 0;
distance = 50;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.bounds;
UIEdgeInsets inset = scrollView.contentInset;
if (offset.y > self.lastContentOffset.y)
{
scrollAmount++;
}
else
{
scrollAmount--;
}
bool awayFromTop = offset.y > distance + inset.top;
if (awayFromTop && !navbarHidden) {
[[self navigationController] setNavigationBarHidden:YES animated:YES];
navbarHidden = YES;
} else if (!awayFromTop || (scrollAmount < -100)) {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
navbarHidden = NO;
}
self.lastContentOffset = offset;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
scrollAmount = 0;
}
Basically, scrollViewDidScroll counts scroll amount and if user scrolls upward, it is decrementing scrollAmount by minus 1.
And if offset is close enough to top of the screen (!awayFromTop) OR scroll amount is smaller than -100, it is expected to navigation bar is hid.
When I put a NSLog for scrollAmount program runs correct, it is hiding nav. bar when user aways from top or shows when approaches to top and scrollAmount is printed correctly.
But whenever scrollAmount reaches to -100 [[self navigationController] setNavigationBarHidden:NO animated:YES]; is not executed and somehow scrollViewDidScroll is called infinitely I mean program enters an infinite loop. scrollAmount is printed like -100,-101,-102...,-1005...
Then I've used below code:
if ([scrollView.panGestureRecognizer translationInView:self.view].y < heightOfScreen/-4.0f && !navbarHidden) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
navbarHidden = YES;
} else if ([scrollView.panGestureRecognizer translationInView:self.view].y > heightOfScreen/4.0f && navbarHidden) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
navbarHidden = NO;
}
Obviously [scrollView.panGestureRecognizer translationInView:self.view].y gives sth. similar to scrollAmount but it works perfect, now I wonder why my implementation has been failed. Any ideas appreciated.

The code looks right but if I were you, I would use
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
to track the scroll view scrolling, because scrollViewDidScroll generates some weird behavior in my app too. Maybe you'd like to give a try.

Related

Scrolling a UIView along with UITableView scroll

I have a tableview in which UITableViewCell contains a textfield. When user tap on the textfield and keyboard appears, I scroll to the tapped in row after setting proper content inset.
Now, I have a UIView shown on top of my table view which I need to adjust accordingly when table scrolls.
To do this, I implemented scrollViewDidScroll: method as below. While this works in certain situations, it do not work or give random results in some situations. Am I doing something wrong here?
- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.hollowView) {
CGPoint offset = iScrollView.contentOffset;
CGRect hollowViewFrame = self.hollowView.hollowFrame;
hollowViewFrame.origin.y += self.previousOffset - offset.y;
self.previousOffset = offset.y;
[self.hollowView resetToFrame:hollowViewFrame];
}
}
- (void)keyboardDidHide:(NSNotification *)iNotification {
self.previousOffset = 0.0;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
If you want to scroll the both view simultaneously then you should have to take both views with same size and in following method just set the scrollview content offset to them. It will surly work
enter code here - (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.scrollView) // Check for the scrolled view
{
// Set the Tableview offset directly
tableview.contentOffset = iScrollView.contentOffset
}
else {
//Set view scroll as per tableview scroll
view.contentOffset = iScrollView.contentOffset } }
//Note :- Make sure view should be scrollview type or if view is getting scared or shows black screen
Thanks..
Use this library:
https://github.com/hackiftekhar/IQKeyboardManager
It will handle the position of Views when tapped on a textfield.
So, this piece worked for me for anyone who is struggling with same situation:
- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.hollowView) {
CGRect frame = [self.tableView rectForRowAtIndexPath:self.expandedCellIndexPath];
frame.origin.y -= self.tableView.contentOffset.y;
[self.hollowView resetToFrame:frame];
}
}
you can use this for scrolling table in ios.
-(void )keyboardWillShow:(NSNotification *)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin =[keyboardInfovalueForKey:UIKeyboardFrameEndUserInfoKey];
keyBoardHeight = keyboardFrameBegin.CGRectValue.size.height;
vwSendBottomSpaceContraint.constant = keyBoardHeight
[self performSelector:#selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1];
}
-(void)keyboardWillHide:(NSNotification*)notification
{
vwSendBottomSpaceContraint.constant = 0;
[self performSelector:#selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1];
}
-(void)scrollToBottomAnimated
{
NSInteger rows = [tableview numberOfRowsInSection:0];
if(rows > 0)
{
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottomanimated:YES];
}
}

Avoid UIPageViewController "over-scrolling"

I have 2 VCs on my UIPageViewController.
I want that if the user is on the 1st view, he won't be able to swipe to the right (just to the left - to the 2nd view), and if he's on the 2nd view he won't be able to swipe to the left (just to the right - to the 1st view).
I want that the user won't be able to scroll - not a black view (what happens when I return nil on: - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController)
The "black view" is likely to be the background of your main view, or the background colour of your UIPageViewController.
You can kill scrolling in one of the directions depending on the ViewController that the PageViewController is currently showing
first set your VC as the delegate for the PageViewController's inner scroll view. You can add a more sophisticated subviews traversal here, this is a simple version:
for (UIView *view in self.pageViewController.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
[(UIScrollView *)view setDelegate:self];
}
}
then add a property to your VC as this:
#property (nonatomic) CGPoint lastOffset;
and after that implement the following scroll view delegate methods
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
scrollView.scrollEnabled = YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
scrollView.scrollEnabled = YES;
self.lastOffset = scrollView.contentOffset;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint nowOffset = scrollView.contentOffset;
NSLog(#"delta %f", self.lastOffset.x - nowOffset.x);
if ((self.lastOffset.x - nowOffset.x) < 0) {
//uncomment to prevent scroll to left
//scrollView.scrollEnabled = NO;
} else if ((self.lastOffset.x - nowOffset.x) > 0) {
//uncomment to prevent scroll to right
//scrollView.scrollEnabled = NO;
} else {
scrollView.scrollEnabled = YES;
}
}

Move a UIView based on the content offset for UIScrollView

I am trying to pull down a UIView (Like a Pull Down Menu) when the user scrolls below a Y-offset of 0.0 in the UITable View. If the user pulls down below -80.0 Y-Offset, then the PullDownMenu locks itself , until the User scrolls the other way.
My implementation for just the UITableView's ScrollView is as follows : [lock:false initially]
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(isChangingOffset)
return;
if(resetDrag)
{
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
resetDrag = false;
}
float xx = scrollView.contentOffset.y;
NSLog(#"Offset :%f",xx);
if(xx - begginOffset > 0.0 && lock && !doneDragging)
{
offsetChange = xx - begginOffset;
begginOffset = xx;
lock = false;
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
}
if(lock){
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
}
if(xx <=-80.0)
{
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
lock = true;
}
}
- (void)setScrollViewOffset:(UIScrollView *)scrollView offsetTo:(CGPoint)offset{
- (void)setScrollViewOffset:(UIScrollView *)scrollView offsetTo:(CGPoint)offset{
isChangingOffset = true;
scrollView.contentOffset = CGPointMake(0, -80.0);
isChangingOffset = false;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
float x = scrollView.contentOffset.y;
begginOffset = x;
doneDragging = false;
if(lock){
resetDrag = true;
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
doneDragging = true;
}
Here is a working video of how it looks : Video
The blue color is a UIView that I have added as a subview in the UITableView.
My problem is , I am trying to pull down a UIView,lets call it menuView (that is not a subview of the tableView) based on the UITableView's contentOffset. I could have simply added the menuView in the UITableView like I added the blue color view. But that is only accessible via the table, that is when I scroll to the top and drag it down. But I want the menuView to be 'pull-able' like the notification center at any time.
On using the Y-contentOffset of scroll view, the menuview pull down animation is not smooth. And it stops midway or goes too low. It is jerky and not always the same. How can I implement this ?
Here the sample code for your UIScrollView :
ViewController.h
#interface ViewController: UIViewController {
UIScrollView *scrollView;
}
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
ViewController.m
#implementation ViewController
#synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self scrollView];
}
- (void)scrollText{
[scrollView setContentSize:CGSizeMake(320, 800)];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = YES;
}
And iside you can put wat you want, from code or interface builder.
For a PullDownMenu you can see this GitHub:
MBPullDownController
Hope this help you and simplify your code ;)

How to implement the scroll process of tableheaderview?

When I add some hidden header view, like search bar, to the table view, it scrolls automatically to content's top or table cell's top, when I set the offset of the scroll to the middle of a search bar. (I used below code)
// in viewDidLoad section
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]
[self.tableView setTableHeaderView:searchBar]
// in viewWillAppear: section
[self.tableView setContentOffset:CGPointMake(0, 44)];
For example, if I scroll up a little amount when the search bar is hidden, it automatically scrolls to display the entire search bar.
And if I scroll down a little when the search bar is displayed, it automatically scrolls to make the search bar hidden.
I used below code to implement this feature to my collection view's header, but that wasn't exactly the same as the table view's feature.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGFloat offset = scrollView.contentOffset.y;
if (offset > 22 && offset < 44) {
[scrollView setContentOffset:CGPointMake(0, 44) animated:YES];
} else if (offset > 44) {
return;
} else {
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
I think it is very hard to mimic above feature exactly, because the judgment of displaying the entire search bar or hiding the search bar is very subtle.
So my question is, "Is there any pre-implemented method in iOS SDK, or in UICollectionViewController?".
I googled for many hours but I couldn't found the answer.
I couldn't find the pre-implemented way to implement that feature, but the below way seems to make things similar to that of table view's.
First, add CGFloat type ivar originScrollOffset.
Then, I used the below code to implement scroll view delegate.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
originScrollOffset = scrollView.contentOffset.y;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGFloat endScrollOffset = scrollView.contentOffset.y;
NSLog(#"%f %f", originScrollOffset, endScrollOffset);
if (endScrollOffset <= 44 && endScrollOffset >= 0) {
if (originScrollOffset >= 44) {
if (originScrollOffset - endScrollOffset > 10) {
originScrollOffset = 0;
[self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
} else {
originScrollOffset = 44;
[self.collectionView setContentOffset:CGPointMake(0, 44) animated:YES];
}
} else {
if (endScrollOffset - originScrollOffset > 10) {
originScrollOffset = 44;
[self.collectionView setContentOffset:CGPointMake(0, 44) animated:YES];
} else {
originScrollOffset = 0;
[self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
}
}

Several Questions about ScrollView with PageControl

I am pretty new to iOS development and I stumbled upon several issues for which I couldn't easily find any answers yet:
General Setup: I'm using a ScrollView with PageControl inside a TabBarApplication
Is it possible to have the PageControl within the same area as the content of the pages? For me it always gets hidden by the SrollView's Views, but due to display space being rare I really need it on the same height as the actual content.
I've fooled around in some Sandbox-Project and whenever I first started to implement a button into the View of a ScrollView-Page the Pages of the ScrollView wouldn't show immediately anymore, but only after the first scroll attempt. I'd post some code about that but its basically only autogenerated from IB.
This is a general Question about possibilities again: The main design of the Project should be a TabBarApplication with a NavigationController letting you go deeper into sub-menues like it is pretty common. Now in one of the Tabs there should be the PageControl, in which you can then again go into sub-menues by pushing Views on a NavigationController stack . Is this possible?
Some Code for 2.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]]; // [TaskPageViewController new]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
TaskPageViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[TaskPageViewController alloc] init]; //WithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
You can have two view hierarchies for this:
Have the page control inside scrollview with origin fixed at contentOffset property
Have the page control in the superview of scrollView, but at a higher index (i.e. floating above it)
This depends on where you put the code of adding the subviews. Is it in the delegate method of scrollView? viewDidLoad? Somewhere else? Some code might help.
Not sure why you'd need to have a page control when it's a drill-down navigation. Pages are for navigating same level items.

Resources