scrolling a UIScrollView on the click of a button - ios

I would like to know if it is possible for me to scroll a UIScrollView on the click of a UIButton and if so, would someone be able to tell me how to.
currently I am using the below code to see if in the scrollview more content is there to its left and if it is there, display an image which would tell the users that there is more content if they scroll to the left.
I would like to implement a functionality where I add a UIButton instead of the image and when more content is available on the left and when the user clicks the button, the scrollview would scroll to its left.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 {
if (scrollView1.contentOffset.x == scrollView1.contentSize.width - scrollView1.frame.size.width)
{
// reached the right
self.imageView.hidden = YES;
}
else
{
self.imageView.hidden = NO;
}
}
It would be great if someone could help me out on this.

That's pretty easy, I think. Use:
[scrollView1 setContentOffset:CGPointMake(x, 0) animated:YES];
Where X is the value of scrollView1.contentOffset.x + scrollView1.contentSize.width.

for Objective C use:
-(IBAction)adjustScroll:(UIButton *)sender{
CGFloat xOffset = scrollView1.contentOffset.x;
CGFloat yOffset = scrollView1.contentOffset.y;
[scrollView1 setContentOffset:CGPointMake(xOffset + scrollView1.contentSize.width, yOffset) animated:YES];
}
for swift 4.2 use:
#IBAction func adjustScroll(sender:UIButton){
let xOffset = scrollView1.contentOffset.x
let yOffset = scrollView1.contentOffset.y
let customOffset = CGPoint(x: xOffset + scrollView1.contentSize.width, y: yOffset)
scrollView1.setContentOffset(customOffset, animated: true)
}

Related

Uipagecontrol last to first move

Create a Scrollview add UIPagecontrol and load the array values successfully. Put timer for scroll it automatically. Want to show after complete the last array value it want to move the first array value automatically.(Now my code working like get reverse and move to first array value). I need go automatically to first. Help me.
Scrollview page move code
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int width;
width = ScrollViewPage.frame.size.width;
float xPos = scrollView.contentOffset.x+10;
pageControl.currentPage = (int)xPos/width;
}
///Add timer for move automatically using tis code
- (void)loadNextController
{
pageControl.currentPage = (pageControl.currentPage+1)%self->pageControllMutableImageArray.count;
[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES];
}
///UIpagecontrol dot button click target action code
- (IBAction)pageTurn:(id)sender
{
NSUInteger page = pageControl.currentPage;
CGRect frame = ScrollViewPage.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[ScrollViewPage scrollRectToVisible:frame animated:YES];
}
How it possible in my code. help me thanks advance.
You can do something like this,
- (void)loadNextController
{
//put if statement here to check whether it is page object something like,
//have aasume that your first page number is 0. so take count - 1 in if
if(pageControl.currentPage == (pageControllMutableImageArray.count - 1)){
//start from initial. set current page = 0 and contentoffset of first page
}
else {
// your code for next page may be like,
pageControl.currentPage = (pageControl.currentPage+1)%self->pageControllMutableImageArray.count;
[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES];
}
}
Hope this will help. :)
Update 2 :
you can set content offset of first page like:
CGPoint newOffset = CGPointMake(0, 0);
[self.standardScrollView setContentOffset:newOffset animated:YES];

Imitate Facebook hide/show expanding/contracting Navigation Bar

In the new iOS7 Facebook iPhone app, when the user scrolls up the navigationBar gradually hides itself to a point where it completely vanishes. Then when the user scrolls down the navigationBar gradually shows itself.
How would you implement this behavior yourself? I am aware of the following solution but it disappears right away and it isn't tied to the speed of the user's scroll gesture at all.
[navigationController setNavigationBarHidden: YES animated:YES];
I hope this isn't a duplicate as I'm not sure how best to describe the "expanding/contracting" behavior.
The solution given by #peerless is a great start, but it only kicks off an animation whenever dragging begins, without considering the speed of the scroll. This results in a choppier experience than you get in the Facebook app. To match Facebook's behavior, we need to:
hide/show the navbar at a rate that is proportional to the rate of the drag
kick off an animation to completely hide the bar if scrolling stops when the bar is partially hidden
fade the navbar's items as the bar shrinks.
First, you'll need the following property:
#property (nonatomic) CGFloat previousScrollViewYOffset;
And here are the UIScrollViewDelegate methods:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 21;
CGFloat framePercentageHidden = ((20 - frame.origin.y) / (frame.size.height - 1));
CGFloat scrollOffset = scrollView.contentOffset.y;
CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset;
CGFloat scrollHeight = scrollView.frame.size.height;
CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;
if (scrollOffset <= -scrollView.contentInset.top) {
frame.origin.y = 20;
} else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
frame.origin.y = -size;
} else {
frame.origin.y = MIN(20, MAX(-size, frame.origin.y - scrollDiff));
}
[self.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:(1 - framePercentageHidden)];
self.previousScrollViewYOffset = scrollOffset;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self stoppedScrolling];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
if (!decelerate) {
[self stoppedScrolling];
}
}
You'll also need these helper methods:
- (void)stoppedScrolling
{
CGRect frame = self.navigationController.navigationBar.frame;
if (frame.origin.y < 20) {
[self animateNavBarTo:-(frame.size.height - 21)];
}
}
- (void)updateBarButtonItems:(CGFloat)alpha
{
[self.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* item, NSUInteger i, BOOL *stop) {
item.customView.alpha = alpha;
}];
[self.navigationItem.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* item, NSUInteger i, BOOL *stop) {
item.customView.alpha = alpha;
}];
self.navigationItem.titleView.alpha = alpha;
self.navigationController.navigationBar.tintColor = [self.navigationController.navigationBar.tintColor colorWithAlphaComponent:alpha];
}
- (void)animateNavBarTo:(CGFloat)y
{
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat alpha = (frame.origin.y >= y ? 0 : 1);
frame.origin.y = y;
[self.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:alpha];
}];
}
For a slightly different behavior, replace the line that re-positions the bar when scrolling (the else block in scrollViewDidScroll) with this one:
frame.origin.y = MIN(20,
MAX(-size, frame.origin.y -
(frame.size.height * (scrollDiff / scrollHeight))));
This positions the bar based on the last scroll percentage, instead of an absolute amount, which results in a slower fade. The original behavior is more Facebook-like, but I like this one, too.
Note: This solution is iOS 7+ only. Be sure to add the necessary checks if you're supporting older versions of iOS.
EDIT: Only for iOS 8 and above.
You can try use
self.navigationController.hidesBarsOnSwipe = YES;
Works for me.
If your coding in swift you have to use this way (from https://stackoverflow.com/a/27662702/2283308)
navigationController?.hidesBarsOnSwipe = true
Here is one more implementation: TLYShyNavBar v1.0.0 released!
I decided to make my own after trying the solutions provided, and to me, they were either performing poorly, had a a high barrier of entry and boiler plate code, or lacked the extension view beneath the navbar. To use this component, all you have to do is:
self.shyNavBarManager.scrollView = self.scrollView;
Oh, and it is battle tested in our own app.
You can have a look at my GTScrollNavigationBar. I have subclassed UINavigationBar to make it scroll based on the scrolling of a UIScrollView.
Note: If you have an OPAQUE navigation bar, the scrollview must EXPAND as the navigation bar gets HIDDEN. This is exactly what GTScrollNavigationBar does. (Just as in for example Safari on iOS.)
iOS8 includes properties to get the navigation bar hiding for free. There is a WWDC video that demonstrates it, search for "View Controller Advancements in iOS 8".
Example:
class QuotesTableViewController: UITableViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.hidesBarsOnSwipe = true
}
}
Other properties:
class UINavigationController : UIViewController {
//... truncated
/// When the keyboard appears, the navigation controller's navigationBar toolbar will be hidden. The bars will remain hidden when the keyboard dismisses, but a tap in the content area will show them.
#availability(iOS, introduced=8.0)
var hidesBarsWhenKeyboardAppears: Bool
/// When the user swipes, the navigation controller's navigationBar & toolbar will be hidden (on a swipe up) or shown (on a swipe down). The toolbar only participates if it has items.
#availability(iOS, introduced=8.0)
var hidesBarsOnSwipe: Bool
/// The gesture recognizer that triggers if the bars will hide or show due to a swipe. Do not change the delegate or attempt to replace this gesture by overriding this method.
#availability(iOS, introduced=8.0)
var barHideOnSwipeGestureRecognizer: UIPanGestureRecognizer { get }
/// When the UINavigationController's vertical size class is compact, hide the UINavigationBar and UIToolbar. Unhandled taps in the regions that would normally be occupied by these bars will reveal the bars.
#availability(iOS, introduced=8.0)
var hidesBarsWhenVerticallyCompact: Bool
/// When the user taps, the navigation controller's navigationBar & toolbar will be hidden or shown, depending on the hidden state of the navigationBar. The toolbar will only be shown if it has items to display.
#availability(iOS, introduced=8.0)
var hidesBarsOnTap: Bool
/// The gesture recognizer used to recognize if the bars will hide or show due to a tap in content. Do not change the delegate or attempt to replace this gesture by overriding this method.
#availability(iOS, introduced=8.0)
unowned(unsafe) var barHideOnTapGestureRecognizer: UITapGestureRecognizer { get }
}
Found via http://natashatherobot.com/navigation-bar-interactions-ios8/
I have some kind of a quick and dirty solution for that. Haven't made any in-depth testing but here's the idea:
That property will keep all the items in the navbar for my UITableViewController class
#property (strong, nonatomic) NSArray *navBarItems;
In the same UITableViewController class I have:
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
return;
}
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
if(self.navBarItems.count > 0){
[self.navigationController.navigationBar setItems:self.navBarItems];
}
[self.navigationController.navigationBar setFrame:frame];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
return;
}
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 21;
if([scrollView.panGestureRecognizer translationInView:self.view].y < 0)
{
frame.origin.y = -size;
if(self.navigationController.navigationBar.items.count > 0){
self.navBarItems = [self.navigationController.navigationBar.items copy];
[self.navigationController.navigationBar setItems:nil];
}
}
else if([scrollView.panGestureRecognizer translationInView:self.view].y > 0)
{
frame.origin.y = 20;
if(self.navBarItems.count > 0){
[self.navigationController.navigationBar setItems:self.navBarItems];
}
}
[UIView beginAnimations:#"toggleNavBar" context:nil];
[UIView setAnimationDuration:0.2];
[self.navigationController.navigationBar setFrame:frame];
[UIView commitAnimations];
}
That's only for ios >= 7, it's ugly I know but a quick way to achieve this. Any comments/suggestions are welcome :)
This works for iOS 8 and above and ensures that the status bar still retains its background
self.navigationController.hidesBarsOnSwipe = YES;
CGRect statuBarFrame = [UIApplication sharedApplication].statusBarFrame;
UIView *statusbarBg = [[UIView alloc] initWithFrame:statuBarFrame];
statusbarBg.backgroundColor = [UIColor blackColor];
[self.navigationController.view addSubview:statusbarBg];
And if you want to show the nav bar when you tap on the status bar you can do this:
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
self.navigationController.navigationBarHidden = NO;
}
Here is my implementation: SherginScrollableNavigationBar.
In my approach I am using KVO for observing UIScrollView's state, so there is no necessity to use a delegate (and you can use this delegate for whatever else you need).
Please try this solution of mine and let me know why this ain't as good as the previous answers.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if (fabs(velocity.y) > 1)
[self hideTopBar:(velocity.y > 0)];
}
- (void)hideTopBar:(BOOL)hide
{
[self.navigationController setNavigationBarHidden:hide animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:hide withAnimation:UIStatusBarAnimationSlide];
}
One way that I’ve accomplished this is the following.
Register your view controller to be the UIScrollViewDelegate of your UITableView for example.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
From within de UIScrollViewDelegate methods you can get the new contentOffset and translate your UINavigationBar up or down accordingly.
Setting the alpha of the subviews can also be done based on some threshold values and factors you can set and compute.
Hope it helps!
In addition to Iwburk's answer I added the following to fix the alpha issue on non custom navigation bars and to reset the navigation bar in the viewWillDisappear method:
- (void)updateBarButtonItems:(CGFloat)alpha
{
for (UIView *view in self.navigationController.navigationBar.subviews) {
NSString *className = NSStringFromClass([view class]);
if ( ![className isEqualToString:#"_UINavigationBarBackground"] ) {
view.alpha = alpha;
}
}
}
- (void)resetNavigationBar {
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
[self.navigationController.navigationBar setFrame:frame];
[self updateBarButtonItems:1.0f];
}
I was looking for a solution that allowed for any style and any behavior. You'll notice that bar condensing behavior is different in many different apps. And of course, the way the bar looks is totally different between apps.
I created a solution for this issue with https://github.com/bryankeller/BLKFlexibleHeightBar/
You can definine your own behavior rules to control how and when the bar shrinks and grows, and you can define exactly how you want the bar's subviews to react to the bar condensing or growing.
Have a look at my project if you want a lot of flexibility to make whatever kind of header bar you can think up.
I was trying to emulate this behavior in a situation where I needed a customized header sitting about a UITableView. I rolled my own "navigation" bar because this sits below a bunch of other stuff on the page and I wanted the section headers to follow the default "docking" behavior. I think I found a pretty clever and succinct way to adjust a UITableView/UIScrollView together with another object in a style similar to that seen in the Facebook/Instagram/Chrome/etc. apps.
In my .xib file, I have my components loaded into a freeform view: http://imgur.com/0z9yebJ (sorry, don't have the rep to inline images)
Notice that, in the left sidebar, the table is ordered behind the main header view. You can't tell from the screenshot, but it also has the same y position as the main header view. Since it extends out of sight, the contentInset property on the UITableView set to 76 (the height of the main header view).
To make the main header view slide up in unison with the UIScrollView, I use the UIScrollViewDelegate's scrollViewDidScroll methods to perform some calculations and change the UIScrollView's contentInset as well as the main header view's frame.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIEdgeInsets insets = scrollView.contentInset;
//tableViewInsetDelta and tableViewOriginalInsetValue are NSInteger variables that I set to 0 and 76, respectively, in viewDidLoad
tableViewInsetDelta = tableViewOriginalInsetValue + scrollView.contentOffset.y;
insets.top = tableViewOriginalInsetValue - tableViewInsetDelta;
if (scrollView.contentOffset.y > -76 && scrollView.contentOffset.y < 0) {
[scrollView setContentInset:insets];
self.pathTitleContainer.frame = CGRectMake(self.pathTitleContainer.frame.origin.x, 44 - tableViewInsetDelta, self.pathTitleContainer.frame.size.width, self.pathTitleContainer.frame.size.height);
} else if (scrollView.contentOffset.y > 0) {
insets.top = 0;
[scrollView setContentInset:insets];
self.pathTitleContainer.frame = CGRectMake(self.pathTitleContainer.frame.origin.x, -32, self.pathTitleContainer.frame.size.width, self.pathTitleContainer.frame.size.height);
} else if (scrollView.contentOffset.y < -76) {
insets.top = 76;
[scrollView setContentInset:insets];
self.pathTitleContainer.frame = CGRectMake(self.pathTitleContainer.frame.origin.x, 44, self.pathTitleContainer.frame.size.width, self.pathTitleContainer.frame.size.height);
}
}
The first if statement does most of the heavy lifting, but I had to include the other two to handle situations where the user is dragging forcefully and the initial contentOffset values sent to scrollViewDidScroll are outside of the range of the first if statement.
Ultimately, this is working really well for me. I hate loading up my projects with a bunch of bloated subclasses. I can't speak to whether this is the best solution performance-wise (I've always been hesitant to put any code in scrollViewDidScroll since it gets called all the time), but the code footprint is the smallest I've seen in any solution for this problem and it doesn't involve nesting a UITableView in a UIScrollView (Apple advises against this in the documentation and touch events end up a bit funky on the UITableView). Hope this helps someone!
HidingNavigationBar a great project that hides the Navigation Bar and the Tab Bar if you want.
HidingNavigationBar supports hiding/showing of the following view
elements:
UINavigationBar
UINavigationBar and an extension UIView
UINavigationBar and a UIToolbar
UINavigationBar and a UITabBar
https://github.com/tristanhimmelman/HidingNavigationBar
I tried implementing GTScrollNavigationBar but my app required me to modify auto layout constraints. I decided to put an example of my implementation up on GitHub in case anyone else has to do this with auto layout. The other issue I had with most of the other implementations is that people don't set the bounds of the scroll view to avoid the parallax scrolling effect that you create while you scroll and adjust the size of the scrollview simultaneously.
Check out JSCollapsingNavBarViewController if you need to do this with auto layout. I've included two versions, one with the nav bar only and another with a sub-bar below the nav bar which collapses before collapsing the nav bar.
for Swift 4,5 - iOS 11 and above
private var previousScrollViewYOffset: CGFloat = 0
private var firstLoad = true
// to avoid scrollViewDidScroll called when first time view controller load
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
firstLoad = false
}
// MARK: - UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
func stoppedScrolling() {
let frame = self.navigationController?.navigationBar.frame ?? .zero
if frame.origin.y < UIView.statusBarFrame.size.height {
self.animateNavBar(to: -frame.size.height + UIView.statusBarFrame.size.height)
}
}
func updateBarButtonItems(alpha: CGFloat) {
self.navigationItem.leftBarButtonItems?.forEach{ item in
item.customView?.alpha = alpha
}
self.navigationItem.rightBarButtonItems?.forEach{ item in
item.customView?.alpha = alpha
}
self.navigationItem.titleView?.alpha = alpha
self.navigationController?.navigationBar.tintColor = self.navigationController?.navigationBar.tintColor.withAlphaComponent(alpha)
}
func animateNavBar(to y: CGFloat) {
UIView.animate(withDuration: 0.2) {[weak self] in
var frame: CGRect = self?.navigationController?.navigationBar.frame ?? .zero
let alpha: CGFloat = frame.origin.y >= y ? 0 : 1
frame.origin.y = y
self?.navigationController?.navigationBar.frame = frame
self?.updateBarButtonItems(alpha: alpha)
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if firstLoad { return }
var frame = self.navigationController?.navigationBar.frame ?? .zero
let size = frame.size.height - UIView.statusBarFrame.size.height
let framePercentageHidden = (UIView.statusBarFrame.size.height - frame.origin.y) / (frame.size.height - 1)
let scrollOffset = scrollView.contentOffset.y
let scrollDiff = scrollOffset - previousScrollViewYOffset
let scrollHeight = scrollView.frame.size.height
let scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom
if scrollOffset <= -scrollView.contentInset.top {
frame.origin.y = UIView.statusBarFrame.size.height
} else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
frame.origin.y = -size
} else {
frame.origin.y = min(UIView.statusBarFrame.size.height, max(-size, frame.origin.y - scrollDiff))
}
self.navigationController?.navigationBar.frame = frame
self.updateBarButtonItems(alpha: 1 - framePercentageHidden)
self.previousScrollViewYOffset = scrollOffset
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
self.stoppedScrolling()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(!decelerate) {
self.stoppedScrolling()
}
}
}
UIView extension
extension UIView {
public static var statusBarFrame: CGRect {
get {
return UIApplication.shared.statusBarFrame
}
}
}
You should custom navigationItem.titleView to apply set alpha
i tried it with this way, i hope it will help.
just implement the code in delegate method and set to to the desired view/subview
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGRect frame=self.view.frame;
CGRect resultFrame=CGRectZero;
if(scrollView.contentOffset.y==0 || scrollView.contentOffset.y<0){
self.lastContentOffset=0;
self.offset=0;
resultFrame=CGRectMake(0, frame.size.height-(40-self.offset.intValue), frame.size.width, 40-self.offset.intValue);
// Pass the resultFrame
[self showHide:YES withFrame:resultFrame];
}else if (self.lastContentOffset > scrollView.contentOffset.y){
NSNumber *temp=[NSNumber numberWithDouble:self.lastContentOffset-scrollView.contentOffset.y];
if(temp.intValue>40 || self.offset.intValue<temp.intValue){
self.offset=[NSNumber numberWithInt:0];
resultFrame=CGRectMake(0, frame.size.height-(40-self.offset.intValue), frame.size.width, 40-self.offset.intValue);
// Pass the resultFrame
[self showHide:YES withFrame:resultFrame];
}else{
if(temp.intValue>0){
self.offset=[NSNumber numberWithInt:self.offset.intValue-temp.intValue];
resultFrame=CGRectMake(0, frame.size.height-(40-self.offset.intValue), frame.size.width, 40-self.offset.intValue);
// Pass the resultFrame
[self showHide:YES withFrame:resultFrame];
}
}
}else if (self.lastContentOffset < scrollView.contentOffset.y){
NSNumber *temp=[NSNumber numberWithDouble:scrollView.contentOffset.y-self.lastContentOffset];
if(self.offset.intValue>40 || (self.offset.intValue+temp.intValue)>40){
self.offset=[NSNumber numberWithInt:40];
// Pass the resultFrame
[self showHide:NO withFrame:resultFrame];
}else{
self.offset=[NSNumber numberWithInt:self.offset.intValue+temp.intValue];
resultFrame=CGRectMake(0, frame.size.height-(40-self.offset.intValue), frame.size.width, 40-self.offset.intValue);
// Pass the resultFrame
[self showHide:YES withFrame:resultFrame];
}
}
self.lastContentOffset = scrollView.contentOffset.y;
}
-(void)showHide:(Boolean)boolView withFrame:(CGRect)frame{
if(showSRPFilter){
//Assign value of "frame"to any view on which you wan to to perform animation
}else{
//Assign value of "frame"to any view on which you wan to to perform animation
}
}
An extension of #Iwburk 's answer... Instead of changing the origin of the navigation bar, I needed to expand/shrink the size of the navigation bar.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = self.previousRect; // a property set in the init method to hold the initial size of the uinavigationbar
CGFloat size = frame.size.height;
CGFloat framePercentageHidden = ((MINIMUMNAVBARHEIGHT - frame.origin.y) / (frame.size.height - 1));
CGFloat scrollOffset = scrollView.contentOffset.y;
CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset;
CGFloat scrollHeight = scrollView.frame.size.height;
CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;
if (scrollOffset <= -scrollView.contentInset.top) {
frame.origin.y = -MINIMUMNAVBARHEIGHT;
} else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
frame.origin.y = -size;
} else {
frame.origin.y = MIN(-MINIMUMNAVBARHEIGHT, MAX(-size, frame.origin.y - scrollDiff));
}
self.previousRect = CGRectMake(0, frame.origin.y, self.jsExtendedBarView.frame.size.width, 155);
self.layoutConstraintExtendedViewHeight.constant = MAXIMUMNAVBARHEIGHT + frame.origin.y + MINIMUMNAVBARHEIGHT;
[self updateBarButtonItems:(1 - framePercentageHidden)];
self.previousScrollViewYOffset = scrollOffset;
}
It doesn't work with the stoppedScrolling method yet, ill post an update when I have it
All of these approaches seem overly complicated... So naturally, I built my own:
class ViewController: UIViewController, UIScrollViewDelegate {
var originalNavbarHeight:CGFloat = 0.0
var minimumNavbarHeight:CGFloat = 0
weak var scrollView:UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// setup delegates
scrollView.delegate = self
// save the original nav bar height
originalNavbarHeight = navigationController!.navigationBar.height
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// will relayout subviews
view.setNeedsLayout() // calls viewDidLayoutSubviews
}
override func viewDidLayoutSubviews() {
var percentageScrolled = min(scrollView.contentOffset.y / originalNavbarHeight, 1)
navigationController?.navigationBar.height = min(max((1 - percentageScrolled) * originalNavbarHeight, minimumNavbarHeight), originalNavbarHeight)
// re-position and scale scrollview
scrollView.y = navigationController!.navigationBar.height + UIApplication.sharedApplication().statusBarFrame.height
scrollView.height = view.height - scrollView.y
}
override func viewWillDisappear(animated: Bool) {
navigationController?.navigationBar.height = originalNavbarHeight
}
}
I found all answers given in Objective-C. This is my answer in Swift 3. This is very generic code and can be used directly. It works with both UIScrollView and UITableView.
var lastContentOffset: CGPoint? = nil
var maxMinus: CGFloat = -24.0
var maxPlus: CGFloat = 20.0
var initial: CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Alarm Details"
self.lastContentOffset = self.alarmDetailsTableView.contentOffset
initial = maxPlus
}
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
var navigationBarFrame: CGRect = self.navigationController!.navigationBar.frame
let currentOffset = scrollView.contentOffset
if (currentOffset.y > (self.lastContentOffset?.y)!) {
if currentOffset.y > 0 {
initial = initial - fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
}
else if scrollView.contentSize.height < scrollView.frame.size.height {
initial = initial + fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
}
}
else {
if currentOffset.y < scrollView.contentSize.height - scrollView.frame.size.height {
initial = initial + fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
}
else if scrollView.contentSize.height < scrollView.frame.size.height && initial < maxPlus {
initial = initial - fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
}
}
initial = (initial <= maxMinus) ? maxMinus : initial
initial = (initial >= maxPlus) ? maxPlus : initial
navigationBarFrame.origin.y = initial
self.navigationController!.navigationBar.frame = navigationBarFrame
scrollView.frame = CGRect(x: 0.0, y: initial + navigationBarFrame.size.height , width: navigationBarFrame.size.width, height: self.view.frame.size.height - (initial + navigationBarFrame.size.height))
let framePercentageHidden: CGFloat = ((20 - navigationBarFrame.origin.y) / (navigationBarFrame.size.height));
self.lastContentOffset = currentOffset;
self.updateBarButtonItems(alpha: 1 - framePercentageHidden)
}
func updateBarButtonItems(alpha: CGFloat)
{
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray.withAlphaComponent(alpha)]
self.navigationController?.navigationBar.isUserInteractionEnabled = (alpha < 1) ? false: true
guard (self.navigationItem.leftBarButtonItems?.count) != nil else { return }
for (_, value) in self.navigationItem.leftBarButtonItems!.enumerated() {
value.customView?.alpha = alpha
}
guard (self.navigationItem.rightBarButtonItems?.count) != nil else { return }
for (_, value) in (self.navigationItem.rightBarButtonItems?.enumerated())! {
value.customView?.alpha = alpha
}
}
The logic of setting alpha to navigation items is copied from #WayneBurkett answer and rewritten in Swift 3.

Loop UIScrollView with PageController

I've found a good solution for looping my scrollview, but I'm also using a UIPageControl for showing what the actual page's number is.
I change the pageControl.currentPage property when the scrollview scrolls, so if you scroll the scrollview more than a half the pageControl shows the new page.
I'd like to keep this feature, but with my actual code the pageNumber is not correct.
I think I should use this: https://github.com/gblancogarcia/GBInfiniteScrollView
But I can't make it work...
Here's my code:
-(void)rotateIfNecessary{
if(scrollView.contentOffset.x == 0) {
CGPoint newOffset = CGPointMake(scrollView.bounds.size.width+scrollView.contentOffset.x, scrollView.contentOffset.y);
[scrollView setContentOffset:newOffset];
[self rotateViewsRight];
}
else if(scrollView.contentOffset.x == (scrollView.contentSize.width - scrollView.frame.size.width) ) {
CGPoint newOffset = CGPointMake(scrollView.contentOffset.x-scrollView.bounds.size.width, scrollView.contentOffset.y);
[scrollView setContentOffset:newOffset];
[self rotateViewsLeft];
}
}
-(void)rotateViewsRight {
NSMutableArray* controllers = [DashboardFrameUtil getFrameViewControllers];
DashboardFrameViewController *endView = [controllers lastObject];
[controllers removeLastObject];
[controllers insertObject:endView atIndex:0];
[self resizePages];
}
-(void)rotateViewsLeft {
NSMutableArray* controllers = [DashboardFrameUtil getFrameViewControllers];
DashboardFrameViewController *endView = [controllers firstObject];
[controllers removeObjectAtIndex:0];
[controllers addObject:endView];
[self resizePages];
}
Then I call rotateIfNecessary in my scrollViewDidEndDecelerating, everything works fine, but the page calculating.. (when the scrollview resizes itself, it calls the scrollViewDidScroll where I calculate the pageNumber:
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
The pageControl doesn't show the correct page number when the scrollView loops.
Any idea?
Thank you in advance!
Let's start with:
if(scrollView.contentOffset.x == 0) {
CGPoint newOffset = CGPointMake(scrollView.bounds.size.width+scrollView.contentOffset.x, scrollView.contentOffset.y);
...
}
condition is true when scrollView.contentOffset.x == 0, so what for do you add scrollView.contentOffset.x (it's surely = 0) to scrollView.bounds.size.width ?
UPDATED
I didn't run code - there can be some mistakes
If I understand correctly when you, for example, rotate right - last element became first and you want LAST page to be showen?
If yes -- the problem with pages is because you change contentOffset all the time and also change 'data source' of scroll view, so there's no correlation between contentOffset and individual DashboardFrameViewController - same DashboardFrameViewController can be at different offsets in different moments of time. So You have to calculate page N according to something else.
For example:
Somewhere in ivar store initial controllers (with initial order):
- (void)viewDidLoad {
[super viewDidLoad];
_initialControllers = [[DashboardFrameUtil getFrameViewControllers] copy];
}
then you need some method to discover what vc is currently vissible or nearest to center (and store in ivar _currentlyVissibleVC), for example:
- (DashboardFrameViewController *)vissibleVC {
for (DashboardFrameViewController *vc in _initialControllers){
if (CGRectContainsPoint(vc.frame, CGPointMake(scrollView.contentOffset.x + scrollView.bounds.size.width/2, scrollView.contentOffset)){
_currentlyVissibleVC = vc;
return;
}
}
}
then you can calculate page num:
int pageN = [_initialControllers indexOfObject:_currentlyVissibleVC] + 1;
Hope helps

Scrollview wrong scrolling on custom control

I have a custom control that looks like this:
It allows you to pick multiplier from 1 to 10. Inside it is scroll view with multiple UILabels in it. Each label has tag and using the tag I calculate needed positions for label.
The user may either swipe to move this or tap on the number to make it move automatically.
But I have faced the problem - if the view is scrolling and I tap on the number while it is scrolling, it will stop not on the number I tapped on, but on some other number.
When the scroll view is not scrolling taps work fine. Here is relevant code:
This method is needed to stop on the number when it ends scrolling:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSInteger targetIndex = targetContentOffset->x / (self.frame.size.width / 4.0) ;
*targetContentOffset = CGPointMake((targetIndex) * (self.frame.size.width / 4.0), targetContentOffset->y);
if (targetIndex > [self.dataSource numberOfItemsInPicker:self] - 1) {
targetIndex = [self.dataSource numberOfItemsInPicker:self] - 1;
}
if (self.gameName) {
for (int i=0; i<self.labels.count; i++) {
UILabel *label = self.labels[i];
if (i == targetIndex) {
label.textColor = [GeneralHelper colorForGame:self.gameName];
}
else {
if ([self.delegate respondsToSelector:#selector(colorForTextForSmallNumberPicker:)]) {
label.textColor = [self.delegate colorForTextForSmallNumberPicker:self];
}
}
}
}
if (self.delegate) {
[self.delegate smallNumberPickerView:self didPickNumberAtIndex:targetIndex];
}
}
This is the code that determines position of labels in scroll view:
- (CGPoint)offsetForIndex:(NSUInteger)index
{
return CGPointMake(roundf((self.frame.size.width / 4.) * index), 0.);
}
And I think this is the offending code that handles tap gesture recognizer:
- (void)handleTapGesture:(UIGestureRecognizer *)recognizer
{
if (recognizer.state != UIGestureRecognizerStateEnded) {
return;
}
[self.scrollView setContentOffset:[self offsetForIndex:recognizer.view.tag - LABELS_START_TAG] animated:YES];
}
My ideas what happens is that during scrolling frame is wrong, or positions are wrong, but I have no idea how to handle this. I noticed that when I tap I NSLog target offset and it is wrong, and if it is not scrolling - I will get right offset.
What can I do about this?
There was an error in calculations. This line:
return CGPointMake(roundf((self.frame.size.width / 4.) * index), 0.);
It divided by 4 but the labels were actually smaller then 1/4 of the picker, so I got wrong results.

UIRefreshControl at the bottom of the UITableView iOS6?

Is it possibile add UIRefreshControl at the bottom of the UITableView?
I would use it to load more data.
Please, Any suggest?
I believe there won't be any simple solution to this problem. May be someone could write a separate library to implement this behaviour plus it will cause more complications once you cache data in tableview.
But let us break down the problem and that might help you in achieving what you want. I have used the following code to add more rows in the app:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
NSLog(#"Scroll End Called");
[self fetchMoreEntries];
}
}
Or you could do something like that:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
{
if (!self.isMoreData)
{
self.MoreData = YES;
// call some method that handles more rows
}
}
}
You must have seen such methods in many question as i have described above and certainly not what you have asked for. But what you can do is while the above code in in process to load more data, you can add a subview and show a couple of images similar to what UIRefreshControl offers. Add the images in the subview to be shown as a progress until the above code gets executed. Home this helps.
By the way, i will suggest you not to do that as it will just waste your time for making something so smaller unless you are just doing it for learning purposes.
The following answer is in Xcode 8 and Swift 3.
first declare
var spinner = UIActivityIndicatorView()
than in viewDidLoad method write the following code:
spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
spinner.stopAnimating()
spinner.hidesWhenStopped = true
spinner.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 60)
tableView.tableFooterView = spinner
now finally override the scrollViewDidEndDragging delegate method with following:
override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
let offset = scrollView.contentOffset
let bounds = scrollView.bounds
let size = scrollView.contentSize
let inset = scrollView.contentInset
let y = offset.y + bounds.size.height - inset.bottom
let h = size.height
let reloadDistance = CGFloat(30.0)
if y > h + reloadDistance {
print("fetch more data")
spinner.startAnimating()
}
}
You can use UIView to customize your refreshControl at bottom. Create UIView and add it to UITableView as footerView.
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"refreshImage.png"]]];
tableView.tableFooterView = footerView;
Hide it: tableView.tableFooterView.hidden = YES;
Implement UIScrollView delegate -
(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
{
tableView.tableFooterView.hidden = NO;
// call method to add data to tableView
}
}
before adding data to tableView save current offset by CGPoint offset = tableView.contentOffset;
call reloadData then set previously saved offset back to tableView [tableView setContentOffset:offset animated:NO];
So that you can feel new data added at bottom.
I've been stuck on the same problem recently and write a category for UIScrollView class. Here it is CCBottomRefreshControl.
Actually the free Sensible TableView framework does provide this functionality out of the box. You specify the batch number and it will automatically fetch the data, displaying the last cell as a 'load more' cell. Once you click that cell, it will automatically load the next batch, and so on. Worth taking a look at.
For UITableView I suggest the following approach:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// if this is the last row that we have in local data
// and we didn't reach the total count of rows from the server side
if (count == indexPath.row+1 && count < total)
{
// .. fetch more rows and reload table when data comes
}
}
I didn't use the scroll view methods intentionally because the scroll view is slow. It happens that scroll view scrolls, we request more data, refresh more rows, and scroll view is not yet finished with scrolling, it is still decelerating and causing problems with fetching more data.
This Swift library add pull to refresh to the bottom of UITableview.
https://github.com/marwendoukh/PullUpToRefresh-iOS
I hope this help you.
Use CCBottomRefreshControl in cocoapods,
As it is for language objective.
you can add the pod file to your swift project then run it,
i have done it, no problem occurs for me
Hope it helps.

Resources