问题
I am attempting to create a custom subclass of the PDFView
class in XCode. I add a PDFView
instance to my window in InterfaceBuiler and create the following files for the subclass:
MyPDFView.h:
#import <Quartz/Quartz.h>
@interface MyPDFView : PDFView
-(void)awakeFromNib;
-(void)mouseDown:(NSEvent *)theEvent;
@end
MyPDFView.m:
#import "MyPDFView.h"
@implementation MyPDFView
-(void)awakeFromNib
{
[self setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable|NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin];
[self setAutoScales:YES];
}
- (void)mouseDown:(NSEvent *)theEvent
{
unsigned long mask = [self autoresizingMask];
NSLog(@"self autoresizingMask: %lu",mask);
NSLog(@"NSViewHeightSizable: %lu",mask & NSViewHeightSizable);
NSLog(@"NSViewWidthSizable: %lu",mask & NSViewWidthSizable);
NSLog(@"self setAutoScales: %@",[self autoScales] ? @"YES" : @"NO");
NSView* sv = [self superview];
NSLog(@"superview autoresizesSubviews: %@",[sv autoresizesSubviews] ? @"YES" : @"NO");
NSSize frame_dims = [self frame].size;
NSLog(@"Frame: (%f,%f)",frame_dims.width,frame_dims.height);
NSSize bounds_dims = [self bounds].size;
NSLog(@"Bounds: (%f,%f)",bounds_dims.width,bounds_dims.height);
NSSize sv_frame_dims = [sv frame].size;
NSLog(@"Superview Frame: (%f,%f)",sv_frame_dims.width,sv_frame_dims.height);
NSSize sv_bounds_dims = [sv bounds].size;
NSLog(@"Superview Bounds: (%f,%f)",sv_bounds_dims.width,sv_bounds_dims.height);
[super mouseDown:theEvent];
}
@end
However, despite setting everything appropriately and the subsequent NSLog
statements that trigger when the PDFView
area is clicked confirming that the object SHOULD be resizing, resizing the window does not resize the PDFView
. Can anyone explain what I need to do to make the PDFView
area scale with the size of the parent window?
The full code for this project that will allow you to build and run it is here:
https://github.com/samuelmanzer/MyPDFViewer
回答1:
I understand that your requirement is to resize the PDFView
as you resize the parent window. There are two ways of achieving this
- Setting The Autoresizing Mask
- Even though you have programatically set the Autoresizing mask, this isn’t effective as autolayout has been turned on for your views (When you create projects by default on Xcode 5, the xib files are by default set for autolayout ). Turn off the Autolayout feature by unchecking the “Use Autolayout” checkbox in the "Identity and Type" tab in the Utilities pane in Xcode for the MainMenu.xib file.
- Modify the MyPDFView's -(void)awakeFromNib by adding the line of code
[self setFrame:[self superview].bounds];
- Using Autolayout by defining layout constraints
- This can be done in the Interface Builder or through code programatically. Please do read Apple Cocoa Auto Layout Guide
来源:https://stackoverflow.com/questions/20037327/cocoa-pdfview-does-not-resize