I had one of my colleagues come to me today with a problem of how can he load or view-swap a documentView
of an NSScrollView
so that the loaded view appears to fix to the top-left corner instead of the bottom-let corner.
He had spent a while searching the web and floundering and didn't have a solution despite reading the documentation at Apple, on StackOverflow and various other places.
Here's the issue piece-by-piece:
In Interface Builder drag an NSScrollView
into the project.
Also in Interface Builder drag two custom views into the project and add some textfields, buttons etc.
Create a controller class (e.g. myController) with the following IBOutlet
s:
IBOutlet NSScrollView * myScrollView
IBOutlet NSView * myCustomView1
IBOutlet NSView * myCustomView2
Connect the outlets to the controls in Interface Builder.
Create an NSView
subclass to flip the documentView
:
@implementation myFlippedView
-(id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
-(void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
}
-(BOOL)isFlipped {
return YES;
}
Select the documentView
of the NSScrollView
in Interface Builder and make it a subclass of myFlippedView
. In Interface Builder you would select the NSScrollView and then click in it again to get to the documentView
, or change the IB Library to show a tree view and select the child custom view of the NSScrollView
.
In the myController
class swap the views with the following methods:
-(void)awakeFromNib {
[myScrollView setDocumentView:myCustomView1];
}
-(IBAction)swapViews:(id)sender {
if ([myScrollView documentView] == myCustomView1) {
[myScrollView setDocumentView:myCustomView2];
} else {
[myScrollView setDocumentView:myCustomView1];
}
}
Finally hook up a button in your project to the action swapViews
, build and run.
The problem is that the coordinates were not being resolved as was expected with the isFlipped.