Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:04+00:00 2026-05-26T20:44:04+00:00

I have two instances of NSScrollView both presenting a view on the same content.

  • 0

I have two instances of NSScrollView both presenting a view on the same content. The second scroll view however has a scaled down version of the document view presented in the first scroll view. Both width and height can be individually scaled and the original width – height constraints can be lost, but this is of no importance.

I have the synchronised scrolling working, even taking into account that the second scroll view needs to align its scrolling behaviour based on the scaling. There’s one little snag I’ve been pulling my hairs out over:

  • As both views happily scroll along the smaller view needs to slowly catch up with the larger view, so that they both “arrive” at the end of their document at the same time. Right now this is not happening and the result is that the smaller view is at “end-of-document” before the larger view.

The code for synchronised scrolling is based on the example found in Apple’s documentation titled “Synchronizing Scroll Views”. I have adapted the synchronizedViewContentBoundsDidChange: to the following code:

- (void) synchronizedViewContentBoundsDidChange: (NSNotification *) notification {

    // get the changed content view from the notification
    NSClipView *changedContentView = [notification object];

    // get the origin of the NSClipView of the scroll view that
    // we're watching
    NSPoint changedBoundsOrigin = [changedContentView documentVisibleRect].origin;;

    // get our current origin
    NSPoint curOffset = [[self contentView] bounds].origin;
    NSPoint newOffset = curOffset;

    // scrolling is synchronized in the horizontal plane
    // so only modify the x component of the offset
    // "scale" variable will correct for difference in size between views
    NSSize ownSize = [[self documentView] frame].size;
    NSSize otherSize = [[[self synchronizedScrollView] documentView] frame].size;
    float scale = otherSize.width / ownSize.width;
    newOffset.x = floor(changedBoundsOrigin.x / scale);

    // if our synced position is different from our current
    // position, reposition our content view
    if (!NSEqualPoints(curOffset, changedBoundsOrigin)) {
        // note that a scroll view watching this one will
        // get notified here
        [[self contentView] scrollToPoint:newOffset];
        // we have to tell the NSScrollView to update its
        // scrollers
        [self reflectScrolledClipView:[self contentView]];
    }

}

How would I need to change that code so that the required effect (both scroll bars arriving at an end of document) is achieved?

EDIT: Some clarification as it was confusing when I read it back myself: The smaller view needs to slow down when scrolling the first view reaches the end. This would probably mean re-evaluating that scaling factor… but how?

EDIT 2: I changed the method based on Alex’s suggestion:

    NSScroller *myScroll = [self horizontalScroller];
NSScroller *otherScroll = [[self synchronizedScrollView] horizontalScroller];

//[otherScroll setFloatValue: [myScroll floatValue]];

NSLog(@"My scroller value: %f", [myScroll floatValue]);
NSLog(@"Other scroller value: %f", [otherScroll floatValue]);

// Get the changed content view from the notification.
NSClipView *changedContentView = [notification object];

// Get the origin of the NSClipView of the scroll view that we're watching.
NSPoint changedBoundsOrigin = [changedContentView documentVisibleRect].origin;;

// Get our current origin.
NSPoint curOffset = [[self contentView] bounds].origin;
NSPoint newOffset = curOffset;

// Scrolling is synchronized in the horizontal plane so only modify the x component of the offset.
NSSize ownSize = [[self documentView] frame].size;
newOffset.x = floor(ownSize.width * [otherScroll floatValue]);

// If our synced position is different from our current position, reposition our content view.
if (!NSEqualPoints(curOffset, changedBoundsOrigin)) {
    // Note that a scroll view watching this one will get notified here.
    [[self contentView] scrollToPoint: newOffset];
    // We have to tell the NSScrollView to update its scrollers.
    [self reflectScrolledClipView:[self contentView]];
}

Using this method the smaller view is “overtaken” by the larger view when both scrollers reach a value of 0.7, which is not good. The larger view then scrolls past its end of document.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:44:05+00:00Added an answer on May 26, 2026 at 8:44 pm

    I finally figured it out. The answer from Alex was a good hint but not the full solution as just setting the float value of a scroller doesn’t do anything. That value needs translation to specific coordinates to which the scroll view needs to scroll its contents.

    However, due to differences in size of the scrolled document view, you cannot just simply use this value, as the scaled down view will be overtaken by the “normal” view at some point. This will cause the normal view to scroll past its end of document.

    The second part of the solution was to make the normal sized view wait with scrolling until the scaled down view has scrolled its own width.

    The code:

    // Scrolling is synchronized in the horizontal plane so only modify the x component of the offset.
        NSSize ownSize = [[self documentView] frame].size;
        newOffset.x = MAX(floor(ownSize.width * [otherScroll floatValue] - [self frame].size.width),0);
    

    The waiting is achieved by subtracting the width of the scroll view from the width times the value of the scroller. When the scaled down version is still traversing its first scroll view width of pixels, this calculation will result in a negative offset. Using MAX will prevent strange effects and the original view will quietly wait until the value turns positive and then start its own scrolling. This solution also works when the user resizes the app window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two instances of the same class. The class has a pointer
I have two instances running of same Windows Service. They check the health of
Is it possible for two instances of Object to have the same hashCode() ?
I have two instances of a program that manipulate same Northwind database. When I
I have two instances of the same database. The first db represents data from
I have two instances of a user control on a page. Both have fields
Let's say you have two instances of the same bean type, and you'd like
Say I have two instances of an application, with the same inputs and same
I have two instances of an Address.ascx control in an ASP.NET MVC page. <h1>Shipping
I have two named instances of SQL Server 2008 and am trying to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.