NSScrollview not scrolling programmatically?

Note. Horizontal and vertical scrollers are visible on the screen and work perfectly. But I can not get them to move programmatically.

I am working on a cocoa desktop application. I use NSScrollview in my Mainmenu.xib file, and I create an output from its owner, which is Appdelegate.h. Here is the way out

@property(nonatomic,retain) IBOutlet NSScrollView* scrollview;

When I try to install a new referencing outlet of my NSScrollview from the interface constructor and take the line for the file owner, I see only one option to "delegate". I do not see the scroll output. Therefore, I connect scrollview to the delegate in the file owner (since I cannot see the scroll view).

Now I am trying to do auto-scrolling in my code. Here is the code

 for(int a=0;a<10000;a++)
    {

        NSPoint pointToScrollTo = NSMakePoint ( 100+a,100+a );  // Any point you like.
        [[self.scrollview contentView] scrollToPoint: pointToScrollTo];
        [self.scrollview reflectScrolledClipView: [self.scrollview contentView]];



    }

, . . .

+4
4

NSClipView scrollToPoint: :

- (IBAction)scrollToMid:(id)sender
{
    CGFloat midYPoint = [self.scrollView contentView].frame.size.height/2.0;
    [[self.scrollView contentView] scrollToPoint:NSMakePoint(0.0, midYPoint)];
    [self.scrollView reflectScrolledClipView:[self.scrollView contentView]];
}

, boundsOrigin - . ( NSScrollView, NSClipView )

- (IBAction)scrollToMidAnimated:(id)sender
{
    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:2.0];
    NSClipView* clipView = [self.scrollView contentView];
    NSPoint newOrigin = [clipView bounds].origin;
    newOrigin.y = [self.scrollView contentView].frame.size.height/2.0;
    [[clipView animator] setBoundsOrigin:newOrigin];
    [NSAnimationContext endGrouping];
}
+14

, . , .

-(void)scrollToTop:(NSScrollView *)scrollView
{
    NSPoint newScrollOrigin;
    if ([[scrollView documentView] isFlipped]) {
        newScrollOrigin=NSMakePoint(0.0,0.0);
    } else {
        newScrollOrigin=NSMakePoint(0.0,NSMaxY([[scrollView documentView] frame]) -NSHeight([[scrollView contentView] bounds]));
    }

    [[scrollView documentView] scrollPoint:newScrollOrigin];
}

windowDidLoad, .

-(void)windowDidLoad
{
    [super windowDidLoad];
    [self scrollToTop:_myScroll];
}
+3

. NSApplication. AppDelegate, scrollview, .

NSObject xib AppDelegate. ( xcode , , "AppDelegate" "" "", FileOwner). " " scrollview . IBOutlet AppDelegate.

UPDATE: , for. for , , . NSTimer, , .

, [scrollview documentView], [scrollview contentView]. .

, , , :

[scroll.contentView scrollToPoint:NSMakePoint(0, ((NSView*)scroll.contentView).frame.size.height - scroll.contentSize.height)];
[scroll reflectScrolledClipView: [scroll contentView]];

.

, .

, for, , xib, :

NSPoint pointToScrollTo = NSMakePoint ( 100,100 );  // Any point you like.
[[scrollview contentView] scrollToPoint: pointToScrollTo];
[scrollview reflectScrolledClipView: [scrollview contentView]];

Now run the application, set the scrollview scroll to a random position. Then press the button and check if the scroll is moved.

If this works, then your code is fine, you only need to set the points. It worked for me.

+1
source

I found that if you use NSViewController, you need to do a "scrollPoint" in your method viewWillAppear, not in viewWillLoad. FYI.

+1
source

All Articles