0

我正在尝试PDFView在 XCode 中创建该类的自定义子类。我在 InterfaceBuiler 的窗口中添加了一个PDFView实例,并为子类创建了以下文件:

我的PDFView.h:

#import <Quartz/Quartz.h>

@interface MyPDFView : PDFView

-(void)awakeFromNib;
-(void)mouseDown:(NSEvent *)theEvent;

@end

我的PDFView.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

然而,尽管正确设置了所有内容,并且在单击NSLog该区域时触发的后续语句PDFView确认对象应该调整大小,但调整窗口大小并不会调整PDFView. 谁能解释我需要做什么才能使PDFView区域与父窗口的大小一起缩放?

该项目的完整代码将允许您构建和运行它:

https://github.com/samuelmanzer/MyPDFViewer

4

1 回答 1

2

我了解您的要求是在调整PDFView父窗口大小时调整其大小。有两种方法可以实现这一点

  1. 设置自动调整大小蒙版
    • 即使您以编程方式设置了 Autoresizing 掩码,这也无效,因为您的视图已打开自动布局(当您在 Xcode 5 上默认创建项目时,xib 文件默认设置为 autolayout )。通过取消选中 MainMenu.xib 文件的 Xcode 实用程序窗格中“标识和类型”选项卡中的“使用自动布局”复选框来关闭自动布局功能。
    • 通过添加代码行来修改 MyPDFView 的-(void)awakeFromNib[self setFrame:[self superview].bounds];
  2. 通过定义布局约束来使用 Autolayout
于 2013-11-19T18:03:06.403 回答